简体   繁体   English

如何在Python中添加多个用户输入

[英]How to add multiple user inputs in Python

I'm just starting out with Python and I'm working on a program for an assignment. 我刚开始使用Python,并且正在开发用于分配任务的程序。 The description is that it's for for an electronic device that will be attached to a grocery cart. 描述是它适用于将要连接到杂货店购物车的电子设备。 When the shopper begins shopping, the device will ask the shopper their budget, which is the maximum amount the shopper would like to spend. 购物者开始购物时,设备将询问购物者他们的预算,这是购物者想要花费的最大金额。 It will then ask the shopper to input the cost of each item that they place in the cart. 然后,它将要求购物者输入他们放入购物车中的每件商品的费用。 Each time something is added to the cart, the device will add the cost of the item to a running total, or sum, of the cost of all the items in the cart. 每次将商品添加到购物车时,设备会将商品的成本添加到购物车中所有商品的运行总成本或总和中。 Once the cost of all the items goes over budget, it will alert the shopper that they have spent too much money. 一旦所有商品的成本超过预算,它就会提醒购物者他们花了太多钱。

I've mapped out the code and kind of figured out everything I need to do. 我已经规划了代码,并弄清楚了我需要做的所有事情。 But I can't get it to add the user's multiple inputs correctly. 但是我无法正确添加用户的多个输入。 Ideally it should add the users first input with their second, and third, etc and stop when the user enters ALL DONE. 理想情况下,它应将用户的第一个输入与第二个,第三个输入相加,并在用户输入ALL DONE时停止。

Here's my code so far. 到目前为止,这是我的代码。 Any pointers would be greatly appreciated! 任何指针将不胜感激!

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!")

So what I've done is checked if the input is a number (.isdigit) and if it is, added that to a running total. 因此,我检查了输入的内容是否为数字(.isdigit),然后将其添加到运行总计中。 Your code wouldn't accept 'ALL DONE' because it would only accept integer inputs, so I've changed that too. 您的代码将不接受“ ALL DONE”,因为它仅接受整数输入,因此我也进行了更改。 Finally, I've changed the budget to a float, because that makes more sense with money I would have though. 最后,我将预算更改为浮动预算,因为用我的钱来说这更有意义。 Hope this helps! 希望这可以帮助! EDIT: It doesn't like floats as a cost, but other than that I've tested it and it seems to work 编辑:它不喜欢浮动作为成本,但除此之外,我已经对其进行了测试,它似乎可以工作

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM