【问题标题】:TypeError: input expected at most 1 arguments, got 3 - AgainTypeError:最多输入 1 个参数,得到 3 个 - 再次
【发布时间】:2019-10-20 08:17:29
【问题描述】:

第 3 行,在
reduce_price=int(input("初始价格为:" ,age, "%"))
TypeError:最多输入 1 个参数,得到 3 个:

age=int(input("The age of the customer is : "))
initial_price=float(input("The initial price est : "))
reduced_price=int(input("The reduced price is : " ,age, "%"))
print("The price after reduction is", initial_price-reduced_price, "$")

【问题讨论】:

  • 我不确定你打算在第 3 行做什么。对于input(),你只能有一个参数,即它打印给用户的文本。您也有导致错误的,age, "%"。我不确定你想对年龄和 % 做什么。
  • 你好@MyNameIsCaleb。对于年龄,我给你举个例子:如果一个 22 岁的客户会有 22% 的折扣。
  • 谢谢,我为您添加了一个解决方案,让您可以在注释行中添加解释。
  • 看起来在第 3 行你想使用 print 而不是 input

标签: python typeerror


【解决方案1】:

这是一个修复。

对于第 3 行,您不需要输入,因为您从用户那里获得了所需的所有信息。您需要做的就是进行所需的数学运算,将年龄计算为百分比,然后乘以价格。

然后在打印行中,您可以进行与之前相同的缩减(但修正了一个错字)。

# I changed this to float rather than int since we are dealing with floats
age = float(input("The age of the customer is : "))
initial_price=float(input("The initial price est : "))
# round(number, decimal_place) to make it a money amount rounded
# initial price * age/100 (age as a percent)
reduced_price=round((initial_price * (age/100)),2)
# this is an f string for Python 3.6+
print(f"The price after reduction is {initial_price-reduced_price}")

在实践中:

>>> age = float(input("The age of the customer is : "))
The age of the customer is : 22
>>> initial_price=float(input("The initial price est : "))
The initial price est : 75
>>> reduced_price=round((initial_price * (age/100)),2)
>>> print(f"The price after reduction is {initial_price-reduced_price}")
The price after reduction is 58.5

【讨论】:

    猜你喜欢
    • 2014-08-01
    • 2012-04-15
    • 1970-01-01
    • 1970-01-01
    • 2022-11-17
    • 2020-02-08
    • 2020-06-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多