Python 3 "Too many arguments"?
Python 3 "Too many arguments"?
我正在为学校写 python quiz/calculator,但我在为测验创建随机问题时遇到了问题。
目前我的代码如下:
while True:
if i + n == 10:
answer = input("If I have",i,"blue marbles and",n,"yellow marbles in a bag, what percentage of blue marbles are there? ")
else:
i = random.randint(1, 10)
n = random.randint(1, 10)
但是python出现错误:
Traceback (most recent call last):
File "<stdin>", line 3, in <module>
TypeError: input expected at most 1 arguments, got 5
我尝试了多种解决方法,例如:
while True:
question = "If I have",i,"blue marbles and",n,"yellow marbles in a bag, what percentage of blue marbles are there? "
answer = input(question)
然而,当在 Shell 上打印时,它会打印 'If i have,4 [for example],'blue marbles and',6,'yellow marbles in a bag, what percentage of blue marbles are there?'.
如您所见,它打印引号和逗号,但正确包含变量 i
和 n
。我看过 .strip()
函数,但所有教程都太混乱了,但我认为这就是我需要的!
任何人都可以使这行代码工作,它只是将问题打印为输入语句,同时包含变量吗?
input()
只接受 一个 参数,但你传入了 5:
input(
"If I have",
i,
"blue marbles and",
n,
"yellow marbles in a bag, what percentage of blue marbles are there? ")
首先将它们连接成一个字符串:
input("If I have {} blue marbles and {} yellow marbles in a bag, "
"what percentage of blue marbles are there? ".format(i, n))
这使用 str.format()
method 将 i
和 n
的值插入到更大的字符串中。
您可能对此处的 print()
感到困惑,它显式地接受任意数量的参数并将每个参数转换为字符串,然后再将它们写入标准输出,并用 space 分隔每个参数. input()
不提供此功能。
我认为您的问题是您将 input
函数调用视为 print
调用。但是,input
和 print
的界面不同。
input
只需要一个参数
print
将接受多个参数
因此,尝试像这样创建提示:
prompt = "If I have {} blue marbles and {} yellow marbles etc".format(i, n)
input(prompt)
答案已经给出;但我会提供一些见解,因为你看起来像个初学者。 Input() 和 Print() 是 built-in 函数。它们与您自己创建的函数没有什么不同。函数基本上是一段代码,调用时为 运行。函数有参数,这些参数是您传递给函数的值。所以我的添加函数将如下所示:
def add(x, y)
answer = x + y
print(answer)
我会通过键入来调用它:
add(10, 20)
而 program/interactive shell 会 return 30.
这就是 Input() 和 Print() 行为不同的原因,因为 Print() 需要接受许多参数并将它们连接成一个大字符串,而 Input 只接受一个。如果您遇到困难,或者不太确定如何使用某个函数、模块等。Python 有一个内置的 help() 函数(需要一个参数,哈哈)。
所以输入 help(input) 会告诉你 input 只会接受一个参数。如果您忘记了某些内容的确切语法或细节,这将非常有用。
希望这能解决您的任何问题,尤其是当您不确定为什么给定的答案有效而您的答案无效时。
我正在为学校写 python quiz/calculator,但我在为测验创建随机问题时遇到了问题。
目前我的代码如下:
while True:
if i + n == 10:
answer = input("If I have",i,"blue marbles and",n,"yellow marbles in a bag, what percentage of blue marbles are there? ")
else:
i = random.randint(1, 10)
n = random.randint(1, 10)
但是python出现错误:
Traceback (most recent call last):
File "<stdin>", line 3, in <module>
TypeError: input expected at most 1 arguments, got 5
我尝试了多种解决方法,例如:
while True:
question = "If I have",i,"blue marbles and",n,"yellow marbles in a bag, what percentage of blue marbles are there? "
answer = input(question)
然而,当在 Shell 上打印时,它会打印 'If i have,4 [for example],'blue marbles and',6,'yellow marbles in a bag, what percentage of blue marbles are there?'.
如您所见,它打印引号和逗号,但正确包含变量 i
和 n
。我看过 .strip()
函数,但所有教程都太混乱了,但我认为这就是我需要的!
任何人都可以使这行代码工作,它只是将问题打印为输入语句,同时包含变量吗?
input()
只接受 一个 参数,但你传入了 5:
input(
"If I have",
i,
"blue marbles and",
n,
"yellow marbles in a bag, what percentage of blue marbles are there? ")
首先将它们连接成一个字符串:
input("If I have {} blue marbles and {} yellow marbles in a bag, "
"what percentage of blue marbles are there? ".format(i, n))
这使用 str.format()
method 将 i
和 n
的值插入到更大的字符串中。
您可能对此处的 print()
感到困惑,它显式地接受任意数量的参数并将每个参数转换为字符串,然后再将它们写入标准输出,并用 space 分隔每个参数. input()
不提供此功能。
我认为您的问题是您将 input
函数调用视为 print
调用。但是,input
和 print
的界面不同。
input
只需要一个参数print
将接受多个参数
因此,尝试像这样创建提示:
prompt = "If I have {} blue marbles and {} yellow marbles etc".format(i, n)
input(prompt)
答案已经给出;但我会提供一些见解,因为你看起来像个初学者。 Input() 和 Print() 是 built-in 函数。它们与您自己创建的函数没有什么不同。函数基本上是一段代码,调用时为 运行。函数有参数,这些参数是您传递给函数的值。所以我的添加函数将如下所示:
def add(x, y)
answer = x + y
print(answer)
我会通过键入来调用它:
add(10, 20)
而 program/interactive shell 会 return 30.
这就是 Input() 和 Print() 行为不同的原因,因为 Print() 需要接受许多参数并将它们连接成一个大字符串,而 Input 只接受一个。如果您遇到困难,或者不太确定如何使用某个函数、模块等。Python 有一个内置的 help() 函数(需要一个参数,哈哈)。
所以输入 help(input) 会告诉你 input 只会接受一个参数。如果您忘记了某些内容的确切语法或细节,这将非常有用。
希望这能解决您的任何问题,尤其是当您不确定为什么给定的答案有效而您的答案无效时。