问题在于: 企业根据购买的单位数量和单价应用折扣。编写一个应用程序,询问用户单位数量和单价,并计算和打印总价格,应用的折扣和总折扣价格。如果单位数量为零或更少,或者每单位价格为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语句中的范围函数不能有浮点数,同时我的值需要小数,我的问题是,这里是否有替代范围函数?
答案 0 :(得分:2)
您在表格中的比较
transform
不起作用,因为if unit_price in range(10.01, 100):
只接受整数参数。相反,您可以使用比较链来编写
range
另请注意,范围中的if 10.01 <= unit_price < 100:
参数是 exclusive ,因此我在该比较中使用了to
。但是,在您的情况下,您可能需要检查< 100
,因为在以下<= 100
中您测试elif
。另请注意,由于浮点数学,值可能介于>= 100.1
和100.0
之间,即使您只使用100.1
的增量,因此您可能希望测试{{1以下0.1
中的<= 100
和if
。