Urb*_*nor 2 python line syntax-error
当运行此代码时,它出现错误,第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))
Run Code Online (Sandbox Code Playgroud)
导致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
Run Code Online (Sandbox Code Playgroud)
问题是python input()函数只准备接受一个参数 - 提示字符串,但你传递了三个.要解决此问题,您只需将所有三个部分合并为一个部分.
您可以使用%运算符格式化字符串:
y = int(input("What power would you like to raise %d to?\n" %x,))
Run Code Online (Sandbox Code Playgroud)
或者使用新的方式:
y = int(input("What power would you like to raise {0} to?\n".format(x)))
Run Code Online (Sandbox Code Playgroud)
您可以在此处找到该文档.