0

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!!!")
  • 2
    Does this answer your question? [How to create a "singleton" tuple with only one element](https://stackoverflow.com/questions/12876177/how-to-create-a-singleton-tuple-with-only-one-element) – Jorge Luis May 24 '23 at 13:00

1 Answers1

0

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.