gpt4 book ai didi

python - 类型错误 : input expected at most1 arguments got 3

转载 作者:太空宇宙 更新时间:2023-11-04 08:10:02 25 4
gpt4 key购买 nike

嗨,有人可以帮我解释一下哪里出了问题吗?

>Traceback (most recent call last):
> File "/Users/admin/Documents/Python Scripts/Stuff I do when bored/Guessing game.py", line >.13, in <module>
> guess = int(input("Hi",name,"you will need to guess a number between 1-10"))
>TypeError: input expected at most 1 arguments, got 3

代码

import random


guesses_taken = 0

print("Welcome to the number guessing game")
name = input("Hi what is your name?")
random_number = random.randint(1,10)



while guesses_taken < 3:
guess = int(input("Hi",name,"you will need to guess a number between 1-10"))
if guess in random_number:
print("Well done you guessed it correctly!")
exit()

elif guess not in random_number:
guesses_taken = guesses_taken + 1
print("Unlucky! Try again!")

if guesses_taken >= 3:
print("Unlucky! Guess it's game over now!")
exit()

最佳答案

你可以使用字符串的.format方法:

"Hi, {}, you will need to guess a number between 1-10".format(name)

问题是您向 input() 函数提供以逗号分隔的值。这告诉函数将这些值作为单独的参数。由于 input 只接受一个参数(提示),因此会引发错误。

关于python - 类型错误 : input expected at most1 arguments got 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24170882/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com