[英]TypeError Too many Arguments
When running this code it appears with an error that there are too many arguments in line 8. I'm unsure on how to fix it. 运行此代码时,出现错误,第8行中有太多参数。我不确定如何修复它。
#Defining a function to raise the first to the power of the second.
def power_value(x,y):
return x**y
##Testing 'power_value' function
#Getting the users inputs
x = int(input("What is the first number?\n"))
y = int(input("What power would you like to raise",x,"to?\n"))
#Printing the result
print (x,"to the power of",y,"is:",power_value(x,y))
Resulting in a TypeError... 导致TypeError ...
Traceback (most recent call last):
File "C:\[bla location]", line 8, in <module>
y = int(input("What power would you like to raise",x,"to?\n"))
TypeError: input expected at most 1 arguments, got 3
The issue is that the python input() function was only ready to accept one parameter - the prompt string, but you passed in three. 问题是python input()函数只准备接受一个参数 - 提示字符串,但你传递了三个。 To solve this issue, you just need to combine all three pieces into one.
要解决此问题,您只需将所有三个部分合并为一个部分。
You can use the %
operator to format string: 您可以使用
%
运算符格式化字符串:
y = int(input("What power would you like to raise %d to?\n" %x,))
Or use the new way: 或者使用新的方式:
y = int(input("What power would you like to raise {0} to?\n".format(x)))
Change your y
input line to 将您的
y
输入行更改为
y = int(input("What power would you like to raise" + str(x) + "to?\n"))
So you will concatenate the three substrings into a single string. 因此,您将三个子字符串连接成一个字符串。
input
accepts one argument which it prints to the screen. input
接受一个打印到屏幕的参数。 You can read about input()
here In your case you are providing 3 arguments to it -> 你可以在这里阅读
input()
在你的情况下,你提供3个参数 - >
"What power would you like to raise"
"What power would you like to raise"
x
x
"to?\\n"
"to?\\n"
You can combine these three things together like this and form one argument 您可以将这三个事物组合在一起,形成一个参数
y = int(input("What power would you like to raise"+str(x)+"to?\n"))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.