TypeError:最多1个参数的输入得到3

时间:2014-06-11 19:17:38

标签: python python-3.x error-handling

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

>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()

4 个答案:

答案 0 :(得分:3)

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

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

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

答案 1 :(得分:1)

应该看起来像

guess = int(input("Hi"+name+"you will need to guess a number between 1-10"))

答案 2 :(得分:1)

您需要使用+

连接字符串
guess = int(input("Hi" + name + "you will need to guess a number between 1-10"))

因为输入是一种方法,当你使用逗号时,它会认为你输入三个参数,而不是一个连接的字符串

答案 3 :(得分:1)

错误消息准确地告诉您,发生了什么:

  

&#34; 输入最多需要1个参数,得到3&#34;

在第13行的某处有一个函数input()

  输入预计最多1个参数,得到3&#34;

调用该函数的典型方法是input(string_to_show),因此它只是一个变量的函数,请参阅文档:https://docs.python.org/2/library/functions.html#input

  

&#34;输入最多需要1个参数,得到3 &#34;

你似乎以某种方式调用函数:input(a, b, c),所以你传递三个变量而不是一个。这是一个错误

正如其他人所指出的那样,您需要将单词abc连接成一个单独的字符串变量,以便将其传递给input()。用户@bern建议使用format()这样做的好方法。