太多参数的Python错误
问题描述:
运行此代码时,它显示一个错误,表示第8行中的参数太多。我不确定如何解决它。太多参数的Python错误
#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))
这里是错误...
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
答
您y
输入线改为
y = int(input("What power would you like to raise" + str(x) + "to?\n"))
所以,你将三个子连接成一个字符串。
答
input
接受一个参数,它打印到屏幕上。你可以阅读有关input()
here 在你的情况你提供3个参数给它 - >
- 弦乐
"What power would you like to raise"
- 整数
x
- 弦乐
"to?\n"
您可以结合这些三件事情在一起,形成一个论点
y = int(input("What power would you like to raise"+str(x)+"to?\n"))
答
你需要指定x
变量:使用format
y = int(input("What power would you like to raise {}to?\n".format(x)))
或
y = int(input("What power would you like to raise %d to?\n"%x)))
答
的问题是,蟒蛇输入()函数是只愿意接受一个参数
- 提示字符串,但你传入三个。为了解决这个问题,你只需要将所有三个部分合并为一个。
可以使用%
运营商格式字符串:
y = int(input("What power would you like to raise %d to?\n" %x,))
或者用新的方式:
y = int(input("What power would you like to raise {0} to?\n".format(x)))
您可以找到该文件here。
+0
谢谢,它只是我不确定'%'操作符如何工作 – 2014-09-25 19:22:35
是的,当它应该是这样的时候,我已经用昏迷来分开它们。谢谢:D – 2014-09-24 16:49:05
不,不应该是这样的。像其他人所建议的那样使用'format'。 – Matthias 2014-09-24 18:24:59
@Matthias“它不应该”?这是为什么?它工作正常。 – CoryKramer 2014-09-24 18:41:01