Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError Too many Arguments [duplicate]

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
like image 571
UrbanConor Avatar asked Aug 18 '26 21:08

UrbanConor


2 Answers

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.

like image 125
Peter Pei Guo Avatar answered Aug 20 '26 12:08

Peter Pei Guo


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.

like image 29
Cory Kramer Avatar answered Aug 20 '26 11:08

Cory Kramer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!