gpt4 book ai didi

python - TypeError 参数太多

转载 作者:行者123 更新时间:2023-11-28 20:21:51 24 4
gpt4 key购买 nike

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

导致类型错误...

     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

最佳答案

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

关于python - TypeError 参数太多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26022250/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com