TypeError参数过多 [英] TypeError Too many Arguments
本文介绍了TypeError参数过多的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
运行此代码时,出现错误,即第8行中的参数过多.我不确定如何修复它.
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.
#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))
导致TypeError ...
Resulting in a 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
推荐答案
问题是python input()函数仅准备接受一个参数-提示字符串,但您传入了三个.要解决此问题,您只需要将所有三个部分组合为一个即可.
The issue is that the python input() function was only ready to accept one parameter - the prompt string, but you passed in three. 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,))
或使用新方法:
y = int(input("What power would you like to raise {0} to?\n".format(x)))
您可以在此处找到文件.
这篇关于TypeError参数过多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文