TypeError参数太多

时间:2014-09-24 16:43:59

标签: 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 = 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 ...

     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

4 个答案:

答案 0 :(得分:5)

问题是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

答案 1 :(得分:2)

y输入行更改为

y = int(input("What power would you like to raise" + str(x) + "to?\n"))

因此,您将三个子字符串连接成一个字符串。

答案 2 :(得分:1)

您需要指定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)))

答案 3 :(得分:0)

input接受打印到屏幕的一个参数。您可以阅读input() here 在你的情况下,你提供3个参数 - &gt;

  1. 字符串"What power would you like to raise"
  2. 整数x
  3. 字符串"to?\n"
  4. 你可以像这样把这三件事结合在一起,形成一个论点

    y = int(input("What power would you like to raise"+str(x)+"to?\n"))