运行此代码时,出现错误,第 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))
导致类型错误...
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() 函数只准备接受一个参数 - 提示字符串,但你传入了三个参数。要解决这个问题,您只需将所有三个部分合并为一个即可。
您可以使用%
格式化字符串的运算符:
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.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)