简体   繁体   English

TypeError参数太多

[英]TypeError Too many Arguments

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. 运行此代码时,出现错误,第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))

Resulting in a TypeError... 导致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

The issue is that the python input() function was only ready to accept one parameter - the prompt string, but you passed in three. 问题是python input()函数只准备接受一个参数 - 提示字符串,但你传递了三个。 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 . 您可以在此处找到该文档。

Change your y input line to 将您的y输入行更改为

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. 因此,您将三个子字符串连接成一个字符串。

you need to specify x variable : 你需要指定x变量:

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

input accepts one argument which it prints to the screen. input接受一个打印到屏幕的参数。 You can read about input() here In your case you are providing 3 arguments to it -> 你可以在这里阅读input()在你的情况下,你提供3个参数 - >

  1. The String "What power would you like to raise" 字符串"What power would you like to raise"
  2. The integer x 整数x
  3. The String "to?\\n" 字符串"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"))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM