我正在创建一个折扣计算器。我无法在程序中定义变量DiscountedPrice
台式机上的Python 3.5
#programmed by
#Discount Calculator
#Jan 29, 2018
#Explanation of program
print("This program will determine if a discount will be applied based
off the quantity purchased. ")
#State Unit Price Variable
UnitPrice=int(99)
#State Unit Quantity Variable
UnitQuantity=int(input("Please input the number of units purchased. "))
#Calculate Initial Cost
InitialCost=UnitPrice*UnitQuantity
print("Your initial cost will be $",InitialCost)
#State the Total Cost Variable
TotalCost=InitialCost-DiscountedAmount
#Calculate equation to determine if discount will be applied
if UnitQuantity < 10:
DiscountedAmount = 0
print("There will be no discount applied to this purchase.")
elif UnitQuantity >= 10 and UnitQuantity <= 19:
DiscountedAmount = float(0.1) * UnitPrice
print("There will be a 10% discount applied to your purchase of
$",InitialCost)
print ("Your total with discount come to $", TotalCost)
elif UnitQuantity >= 20 and UnitQuantity <= 49:
DiscountedAmount = float(0.2) * UnitPrice
print("There will be a 20% discount applied to your purchase of
$",InitialCost)
print ("Your total with discount come to $", TotalCost)
elif UnitQuantity >= 49 and UnitQuantity <= 99:
DiscountedAmount = float(0.3) * UnitPrice
print("There will be a 30% discount applied to your purchase of
$",InitialCost)
print ("Your total with discount come to $", TotalCost)
else: UnitQuantity >= 100
DiscountedAmount = float(0.4) * UnitPrice
print("There will be a 40% discount applied to your purchase of
$",InitialCost)
print ("Your total with discount come to $", TotalCost)
用户输入后如果他们将获得高达40%的折扣,则应该能够显示
答案 0 :(得分:0)
我相信您是编程的新手。 Python代码中定义的变量不是符号变量(除非您如此定义),因此,当TotalCost
更改时,您不能期望DiscountedAmount
的值会被更新。另外,您需要在使用变量之前对其进行初始化。
在您的代码中,如果不声明变量TotalCost=InitialCost-DiscountedAmount
(请使用DiscountedAmount
),就不能使用DiscountedAmount = 0
。同样,这将是一次计算,并且值将存储在TotalCost
中。在每种TotalCost=InitialCost-DiscountedAmount
情况下,每次DiscountedAmount
的值根据输入而变化时,都需要重新计算if
这个表达式。