2015-10-17 57 views
0

我刚刚开始使用Python,我正在为一个任务编写程序。描述是为了将电子设备连接到购物车。当购物者开始购物时,设备将询问购物者他们的预算,这是购物者想要花费的最大量。然后,它会要求购物者输入他们放在购物车中的每件物品的成本。每次将东西添加到购物车时,设备都会将该物品的成本添加到购物车中所有物品成本的运行总额或总和中。一旦所有物品的成本超过预算,它会提醒购物者他们花了太多钱。如何在Python中添加多个用户输入

我已经绘制出了代码和种类,找出了我需要做的一切。但我无法正确地添加用户的多个输入。理想情况下,它应该添加用户的第一个输入与他们的第二和第三等,并停止当用户进入所有完成。

这是我的代码到目前为止。任何指针将不胜感激!

budget = 0 
itemCost = 0 
cartTotal = 0 

print ("Hello! Welcome to the best grocery store ever!") 
budget = int (input ("What is your budget for today? ")) 
itemCost = int (input ("Please tell me the cost of the most recent item your cart. Print ALL DONE to quit ")) 

while itemCost != "All DONE" and cartTotal <= budget: 
    itemCost = int (input ("Please tell me the cost of the most recent item your cart. Print ALL DONE to quit ")) #works 
    cartTotal = itemCost + itemCost 
    print ("OK, the items in your cart cost a total of ", cartTotal) 
    print ("Your budget is ", budget, " you have spent ", cartTotal, " you have ", budget - cartTotal, " left over.") 
else: 
    print ("You are a horrible budgeter!") 
+0

你不允许数量超过1 –

回答

-1

所以我做了什么,如果输入的是一个数字(.isdigit)被选中,如果是,则补充说,总运行。你的代码不会接受'ALL DONE',因为它只接受整数输入,所以我也改变了。最后,我已将预算更改为浮动,因为这样做对我来说可能更有意义。希望这可以帮助!编辑:它不喜欢花车作为成本,但除了我已经测试它,它似乎工作

budget = 0 
itemCost = 0 
cartTotal = 0 

on = "TRUE" 

print("Hello! Welcome to the best grocery store ever!") 
budget = float(input("What is your budget for today?")) 
while on == "TRUE" : 
    itemCost = input("Please tell me the cost of the most recent item in your cart. Type ALL DONE to quit. ") 
    if itemCost == "ALL DONE" : 
     on = "FALSE" 

    elif itemCost.isdigit() : 
     cartTotal += float(itemCost) 
     if cartTotal < budget : 
      print("Ok, the items in your cart cost a total of ", cartTotal) 
      print ("Your budget is ", budget, " you have spent ", cartTotal, " you have ", budget - cartTotal, " left over.") 
     else : 
      print("You are a horrible budgeter!") 
      break 
    else : 
     print("Invalid entry!") #If neither a number nor ALL DONE was entered, this happens 
     continue 
相关问题