2015-10-17 56 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 
相關問題