Python 3.x 我能';不能定义折扣价格
我正在创建一个折扣计算器。我无法在程序中定义变量DiscountedPrice 桌面上的Python 3.5Python 3.x 我能';不能定义折扣价格,python-3.x,Python 3.x,我正在创建一个折扣计算器。我无法在程序中定义变量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 Pri
#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)
#由编程
#折扣计算器
#2018年1月29日
#程序说明
打印(“此程序将确定是否基于
减去购买的数量。”)
#国家单位价格变量
单价=整数(99)
#状态单位数量变量
UnitQuantity=int(输入(“请输入购买的单位数量”)
#计算初始成本
初始成本=单价*单位数量
打印(“您的初始成本为美元”,初始成本)
#说明总成本变量
总成本=初始成本折扣额
#计算公式以确定是否将应用折扣
如果单位数量小于10:
折扣额=0
打印(“此购买将不享受折扣。”)
elif UnitQuantity>=10,UnitQuantity=20,UnitQuantity=49,UnitQuantity=100
折扣金额=浮动(0.4)*单价
打印(“您购买的产品将享受40%的折扣
$”,初始成本)
打印(“您的折扣总额为美元”,TotalCost)
如果用户输入后可以获得高达40%的折扣,则应能够显示。我相信您对编程非常陌生。Python代码中定义的变量不是符号性的(除非您这样定义),因此当
DiscountedAmount
发生变化时,不能期望TotalCost
的值被更新。此外,您需要在使用变量之前对其进行初始化
在您的代码中,如果不声明变量
DiscountedAmount
(使用DiscountedAmount=0
),则不能使用TotalCost=InitialCost decountedamount
)。此外,这将是一次计算,值将存储在TotalCost
中。每次在每个if
案例中,deffentedamount
的值根据输入发生更改时,您都需要重新计算TotalCost=InitialCost deffentedamount
此表达式。您的错误是什么?是什么阻止您定义该变量?我在你的程序中没有看到任何DiscountedPrice
。很抱歉,它是DiscountedAmount*我不懂python,但看起来你在声明它之前正在使用DiscountedAmount。应该与初始成本或单位数量相同。谢谢,我做了一些更改,除了打印结果而不是1之外,其他一切都正常工作,我正在试图弄清楚为什么会这样做。该程序将确定是否根据购买的数量应用折扣。请输入购买的单位数量。10您的初始成本为990美元购买990美元将享受10%的折扣您的折扣总额为891.0美元购买990美元将享受40%的折扣您的折扣总额为594.0美元