此代码中范围和浮动备选方案的混合

问题描述:

以下是问题: 业务根据购买的单位数量和单位价格应用折扣。编写一个应用程序,询问用户单位数量和单价,并计算并打印总价格,应用的折扣和总折扣价格。如果单位数量为零或更少,或者每单位价格为0欧元或更少,则显示合适的错误消息。此代码中范围和浮动备选方案的混合

这里是我的代码:

units_brought = float(input("Enter the number of units brought ")) 
unit_price = float(input("Enter the unit price ")) 

total_price = units_brought * unit_price 

discount_applied = float(0) 

total_discounted_price = total_price - discount_applied 

if units_brought in range(1, 49) and unit_price in range(0.01, 10) : 
    print("The total price is ",total_price,"\nThe discount applied is",discount_applied,"\nThe total discounted price is ",total_discounted_price) 

    if unit_price in range(10.01, 100) : 
     discount_applied = total_price * 0.05 
     print("The total price is ", total_price, "\nThe discount applied is", discount_applied,"\nThe total discounted price is ", total_discounted_price) 

    elif unit_price >= 100.1: 
      discount_applied= total_price * 0.08 
      print("The total price is ", total_price, "\nThe discount applied is", discount_applied,"\nThe total discounted price is ", total_discounted_price) 

elif units_brought in range(50, 99) and unit_price in range(0.01, 10) : 
    discount_applied = total_price * 0.12 
    print("The total price is ", total_price, "\nThe discount applied is", discount_applied, 
      "\nThe total discounted price is ", total_discounted_price) 

    if unit_price in range(10.01, 100) : 
     discount_applied = total_price * 0.18 
     print("The total price is ", total_price, "\nThe discount applied is", discount_applied, 
       "\nThe total discounted price is ", total_discounted_price) 

    elif unit_price >= 100.01: 
      discount_applied = total_price * 0.22 
      print("The total price is ", total_price, "\nThe discount applied is", discount_applied, 
        "\nThe total discounted price is ", total_discounted_price) 

elif units_brought >= 100 and unit_price in range(0.01, 10) : 
    discount_applied = total_price * 0.21 
    print("The total price is ", total_price, "\nThe discount applied is", discount_applied, 
      "\nThe total discounted price is ", total_discounted_price) 

    if unit_price in range(10.01, 100): 
     discount_applied = total_price * 0.32 
     print("The total price is ", total_price, "\nThe discount applied is", discount_applied, 
       "\nThe total discounted price is ", total_discounted_price) 

    elif unit_price >= 100.01: 
     discount_applied = total_price * 0.43 
     print("The total price is ", total_price, "\nThe discount applied is", discount_applied, 
       "\nThe total discounted price is ", total_discounted_price) 

elif units_brought <= 0: 
    print("The number of units brought can't be zero or less") 

elif unit_price <= 0: 
    print("The unit price can't be zero or less") 

这是当它运行时会发生什么:

Enter the number of units brought 5 
Enter the unit price 4 
Traceback (most recent call last): 
    File "C:/Users/User/Documents/My code/business_price_&_discounts.py", line 19, in <module> 
    if units_brought in range(1, 49) and unit_price in range(0.01, 10) : 
TypeError: 'float' object cannot be interpreted as an integer 

Process finished with exit code 1 

所以,问题似乎是在范围函数的if语句不能有浮动,而在同一时间我的值需要小数,我的问题是,是否有替代范围函数在这里?

您的形式比较

if unit_price in range(10.01, 100): 

不起作用,因为range只需要整数参数。相反,你可以使用比较链接来写

if 10.01 <= unit_price < 100: 

还要注意的是,在一个范围内的to参数是独家,因此,我在比较中使用< 100。但是,在你的情况下,你可能想检查<= 100,因为在下面elif你测试>= 100.1。还要注意的是,由于浮点运算有可能是一个价值在于100.0100.1之间,即使你只使用0.1增量,因此你可能要在以下elif测试在if> 100<= 100