TypeError Too many Arguments

空扰寡人 提交于 2019-12-04 04:50:04

问题


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))

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

回答1:


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,))

Or use the new way:

y = int(input("What power would you like to raise {0} to?\n".format(x)))

You can find the document here.




回答2:


Change your y input line to

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

So you will concatenate the three substrings into a single string.




回答3:


you need to specify x variable :

using format

y = int(input("What power would you like to raise {}to?\n".format(x)))

or

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



回答4:


input accepts one argument which it prints to the screen. You can read about input() here In your case you are providing 3 arguments to it ->

  1. The String "What power would you like to raise"
  2. The integer x
  3. The String "to?\n"

You can combine these three things together like this and form one argument

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


来源:https://stackoverflow.com/questions/26022250/typeerror-too-many-arguments

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!