如何解决元组中的可迭代浮点数

huangapple go评论62阅读模式
英文:

How solve iterable float number in a tuples

问题

我正在尝试使用三个元组,它们分别表示三种不同类型的奖金价格,所以当选择它们中的任何一种类型时,必须指定要购买的奖金类型的数量,并且必须自动计算所选数量的总付款金额。可以对两种基本奖金类型进行操作,但是在计算高级奖金时出现错误:类型为'float'的参数不可迭代,有什么解决办法吗?

basic_bonus_1 = (0.01, 0.02, 0.03)
basic_bonus_2 = (0.04, 0.05, 0.06)
premium_bonus = (1.99)
while True:
    price_bonus = input("请输入奖金价格(或输入quit退出):")
    if price_bonus == 'quit':
        break
    price_bonus = float(price_bonus)
    quantity = int(input("请输入奖金数量:"))
    if price_bonus in basic_bonus_1:
        total = price_bonus * quantity
        print("选择的奖金类型:1,应付金额:", total)
    elif price_bonus in basic_bonus_2:
        total = price_bonus * quantity
        print("选择的奖金类型:2,应付金额:", total)
    elif price_bonus == premium_bonus:
        total = price_bonus * quantity
        print("选择的奖金类型:premium,应付金额:", total)
    else:
        print("抱歉,该值无效!")
英文:

I am trying to use 3 tuples which represent the price of three different types of bonus, so, when choosing any type of them, the quantity of the type of bonus to be purchased must be specified and it must automatically calculate the total amount to be paid for the chosen amount. It can be done with the two types of basic bonuses, but when doing the calculation with the premium bonus it gives me an error: argument of type 'float' is not iterable, any ideas to solve it?

 basic_bonus_1 = (0.01, 0.02, 0.03)
 basic_bonus_2 = (0.04, 0.05, 0.06)
 premium_bonus = (1.99)
 while True:
     price_bonus = input("\nEnter the price of bonus (or quit): ")
     if price_bonus == 'quit':
         break
     price_bonus =  float(price_bonus)
     quantity = int(input("\nEnter the quantity of bonus: "))
     if price_bonus in basic_bonus_1:
        total = price_bonus * quantity
        print("Bono elegido: 1, precio a pagar:", total)
     elif price_bonus in basic_bonus_2:
        total = price_bonus * quantity
        print("Bono elegido: 2, precio a pagar:", total)
     elif price_bonus in premium_bonus:
        total = price_bonus * quantity
        print("Bono elegido: premium, precio a pagar:", total) 
     else:
        print("Sorry, this value is not valid!!!")

答案1

得分: 0

以下是翻译好的内容:

你遇到的错误是因为 premium_bonus 变量被定义为一个单个浮点数值,而不是一个元组。当你检查 price_bonus 是否在 premium_bonus 中时,它会抛出错误,因为它尝试迭代一个单个浮点数值,这是不可迭代的。

要解决这个问题,你可以将 premium_bonus 定义为包含一个单个浮点数值的元组,像这样:

premium_bonus = (1.99,)

在浮点数值后面添加逗号,它就变成了一个包含一个元素的元组。这样你就可以检查 price_bonus 是否在元组中,而不会遇到“float 类型的参数不可迭代”错误。

以下是更新后的代码:

basic_bonus_1 = (0.01, 0.02, 0.03)
basic_bonus_2 = (0.04, 0.05, 0.06)
premium_bonus = (1.99,)

while True:
price_bonus = input("输入奖金的价格(或退出):")
if price_bonus == '退出':
break
price_bonus = float(price_bonus)
quantity = int(input("输入奖金的数量:"))
if price_bonus in basic_bonus_1:
total = price_bonus * quantity
print("选择奖金:1,支付价格:", total)
elif price_bonus in basic_bonus_2:
total = price_bonus * quantity
print("选择奖金:2,支付价格:", total)
elif price_bonus in premium_bonus:
total = price_bonus * quantity
print("选择奖金:高级,支付价格:", total)
else:
print("抱歉,这个数值无效!")

通过这个改变,代码应该能够正确工作,允许你根据指定的数量计算每种奖金的总价格。

英文:

The error you're encountering occurs because the premium_bonus variable is defined as a single float value, not as a tuple. When you check if price_bonus is in premium_bonus, it throws an error because it's trying to iterate over a single float value, which is not iterable.

To fix this issue, you can define premium_bonus as a tuple containing a single float value, like this:

premium_bonus = (1.99,)

By adding a comma after the float value, it becomes a tuple with one element. This allows you to check if price_bonus is in the tuple without encountering the "argument of type 'float' is not iterable" error.

Here's the updated code:

basic_bonus_1 = (0.01, 0.02, 0.03)
basic_bonus_2 = (0.04, 0.05, 0.06)
premium_bonus = (1.99,)

while True:
    price_bonus = input("\nEnter the price of bonus (or quit): ")
    if price_bonus == 'quit':
        break
    price_bonus = float(price_bonus)
    quantity = int(input("\nEnter the quantity of bonus: "))
    if price_bonus in basic_bonus_1:
        total = price_bonus * quantity
        print("Bonus chosen: 1, price to pay:", total)
    elif price_bonus in basic_bonus_2:
        total = price_bonus * quantity
        print("Bonus chosen: 2, price to pay:", total)
    elif price_bonus in premium_bonus:
        total = price_bonus * quantity
        print("Bonus chosen: premium, price to pay:", total)
    else:
        print("Sorry, this value is not valid!")

With this change, the code should work correctly, allowing you to calculate the total price for each type of bonus based on the quantity specified.

huangapple
  • 本文由 发表于 2023年5月24日 20:58:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76323815.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定