我最近刚开始自学Python(3.5.1),并编写了一些基本程序。我正在尝试编写一个程序,允许用户最多购买3种产品。然而,我很难获得插入的总金额的正确值,我猜这是因为它一直在重新初始化自己。感谢您的帮助。另外,我的代码太复杂了吗?如果是,我该如何简化它
更新代码:
def main():
displayPrices()
purchaseItems()
def displayPrices():
print("Item ID:tCost: ")
print("1tt$1.25")
print("2tt$0.75")
print("3tt$0.90")
print("4tt$0.75")
print("5tt$1.50")
print("6tt$0.75")
def purchaseItems():
choice=""
numItems=0
totalCost=0
totalReturned=0
item=0
change=0
x=0
money=float(input("Insert the amount of money you would like to input: $"))
totalInserted=money
while x < 3:
item=int(input("nWhich item would you like to purchase?nIf you would like to quit, enter '0': "))
if item == 1:
cost = 1.25
elif item == 2:
cost = .75
elif item == 3:
cost = .90
elif item == 4:
cost = .75
elif item == 5:
cost = 1.50
elif item == 6:
cost = .75
elif item == 0:
print("Thank you for using our Vending Machine. Goodbye!")
break
else:
print("Error. Please enter a valid Item ID [1-6]")
if cost <= money:
change = money - cost
numItems+=1
money=change
totalCost=totalCost+cost
totalReturned=change
print("Thank you for purchasing Item " + str(item) + ". Your change is $" + str(round(change,2)) + ".n")
x+=1
else:
moneyNeeded = cost - money
print("Please enter an additional $" + str(moneyNeeded) + " to purchase your item.")
newMoney=float(input("Insert the amount of money you would like to input: $"))
totalInserted=money=money+newMoney
# End of Loop
print("Numbers of Items Purchased:", numItems)
print("Total cost of all Items Purchased:", totalCost)
print("Total amount of money inserted:", round(totalInserted,2))
print("Total amount of change returned:", round(totalReturned,2))
main()
查看以下输出:
物料ID:成本:
1$1.25
2$0.75
3$0.90
4$0.75
5$1.50
6$0.75
插入您想要输入的金额:$0
你想购买哪种商品
如果要退出,请输入"0":1
请额外输入1.25美元以购买您的商品
插入您想要输入的金额:$1.00
你想购买哪种商品
如果要退出,请输入"0":2
感谢您购买物品2。您的零钱是0.25美元。
你想购买哪种商品
如果要退出,请输入"0":1
请额外输入1.0美元以购买您的商品
插入您想要输入的金额:$1.0
你想购买哪种商品
如果要退出,请输入"0":1
感谢您购买物品1。您的零钱是0.0美元。
你想购买哪种商品
如果要退出,请输入"0":0
感谢您使用我们的自动售货机。再见
购买的物品数量:2
所有采购项目的总成本:2.0
插入的总金额:1.25---这是不正确的
返回的更改总量:0.0
我很惊讶这一行竟然运行时没有出错,但显然这是一个有效的构造:
totalInserted=money=money+newMoney
在该行中,totalInserted
被分配了money
的值。但是,当前可用的money
与totalInserted
不同。你真正想要的是:
money += newMoney
totalInserted += newMoney
如果你的代码太复杂,一个让它更简单的建议是将商品/价格关系存储在字典中:
prices = {1:1.25, 2:.75, ...}
然后稍后你可以这样做:
cost = prices[item]
您仍然需要首先检查item
是0
还是无效输入。您还有一个从未使用过的变量choice
。
这里可能有一个解决方案。
def main():
displayPrices()
purchaseItems()
def displayPrices():
print("Item ID:tCost: ")
print("1tt$1.25")
print("2tt$0.75")
print("3tt$0.90")
print("4tt$0.75")
print("5tt$1.50")
print("6tt$0.75")
def purchaseItems():
money = float(input("How much money would you like to insert?"))
moneyInserted = money
items_sold = 0
items = [1.25, .75, .9, .75, 1.5, .75]
moneySpent = 0
total_cost = 0
x = 1
while x != 0 and money > 0 and items_sold < 3:
print("You have $" + str(money) + " left.")
x = int(input("What item would you like to buy?"))
if x == 0:
pass
elif x < 1 or x > 6:
print("Please return a valid item number.")
elif money >= items[x-1]:
print("Thank you for buying item "+str(x)+".")
money -= items[x-1]
moneySpent += items[x-1]
total_cost += items[x-1]
items_sold += 1
else:
pass
print("Number of items sold: " + str(items_sold))
print("Money Inserted: " + str(moneyInserted))
print("Amount of money spent: " + str(total_cost))
print("Change amount: " + str(money))
main()