(Python)自动售货机程序返回错误的金额插入?

时间:2016-04-01 23:53:59

标签: python loops python-3.x while-loop

我刚刚开始自学Python(3.5.1)并编写了一些基本程序。我正在尝试编写一个程序,允许用户最多购买3个产品。但是,我无法获得插入总金额的正确值,我猜测是因为它不断重新初始化。任何帮助表示赞赏。另外,我的代码是否太复杂,如果是这样,我该如何简化呢?

更新代码:

def main():
   displayPrices()
   purchaseItems()

def displayPrices():
   print("Item ID:\tCost: ")
   print("1\t\t$1.25")
   print("2\t\t$0.75")
   print("3\t\t$0.90")
   print("4\t\t$0.75")
   print("5\t\t$1.50")
   print("6\t\t$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()

看看以下输出:
物品编号:成本:
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

2 个答案:

答案 0 :(得分:0)

我很惊讶这条线甚至没有错误地运行,但显然它是一个有效的构造:

totalInserted=money=money+newMoney

在该行中totalInserted被赋予money的值。但是,money当前可用与totalInserted不同。你真正想要的是:

money += newMoney
totalInserted += newMoney

至于你的代码是否太复杂,一个简单的建议是将项目/价格关系存储在字典中:

prices = {1:1.25, 2:.75, ...}

然后你可以这样做:

cost = prices[item]

您仍然需要先检查item0还是输入无效。您还有一个从未使用的变量choice

答案 1 :(得分:0)

这可能是一种解决方案。

def main():
   displayPrices()
   purchaseItems()


def displayPrices():
   print("Item ID:\tCost: ")
   print("1\t\t$1.25")
   print("2\t\t$0.75")
   print("3\t\t$0.90")
   print("4\t\t$0.75")
   print("5\t\t$1.50")
   print("6\t\t$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()