Voici le problème: Une entreprise applique des remises basées sur le nombre d'unités achetées et le prix unitaire. Rédiger une demande qui demande à l'utilisateur le nombre d'unités et le prix unitaire et calcule et imprime le prix total, la réduction appliquée et le prix total réduit. Afficher un message d'erreur approprié si le nombre d'unités est égal ou inférieur à zéro ou si le prix unitaire est de 0 € ou moins.Combinaison de variantes de plage et de flottant dans ce code
Voici mon code:
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")
Ce qui se passe quand il fonctionne:
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
Le problème semble être les fonctions de portée dans le cas des déclarations qui ne peuvent pas avoir des flotteurs en en même temps mes valeurs exigeant des décimales, ma question est, Y at-il une alternative à la fonction de gamme ici?