Python TypeError参数太多
运行此代码时,第8行中的参数太多,这会显示一个错误。我不知道如何修理它Python TypeError参数太多,python,syntax-error,line,Python,Syntax Error,Line,运行此代码时,第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
#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
回溯(最近一次呼叫最后一次):
文件“C:\[bla位置]”,第8行,在
y=int(输入(“您希望提高的功率是多少”,x,“到?\n”))
TypeError:输入最多需要1个参数,得到3个
输入
接受打印到屏幕上的一个参数。您可以阅读有关input()
在您的案例中,您提供了3个参数->
“您希望提高的功率是多少”
x
“to?\n”
y = int(input("What power would you like to raise"+str(x)+"to?\n"))
将
y
输入行更改为
y = int(input("What power would you like to raise" + str(x) + "to?\n"))
因此,您将把三个子字符串连接成一个字符串。您需要指定
x
变量:
使用
或
问题是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)))
您可以找到该文档。是的,我已经使用comas将它们分开,而实际上应该是这样的。谢谢:不,不应该是这样的。使用别人建议的
格式
。@Matthias“不应该”?为什么呢?它很好用,但缺乏灵活性。使用格式
可以更好地控制结果。另外:在您的示例中,您缺少数字周围的空格。使用格式字符串,您将立即看到它。请提供更多答案的上下文。此外,我仍然在答案中看到语法错误。谢谢,只是我不确定“%”运算符是如何工作的
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)))