如何将用户输入添加到列表?

问题描述 投票:0回答:1

我对编码很陌生,只有几个月的高中经验,并且即将开展一个项目。对于我的项目,我决定创建一种商店,询问用户想要从水果和蔬菜列表中添加什么到购物车。

这个想法是,当他们输入“苹果”时,答案将被保存以供以后使用。接下来,他们被要求输入他们选择的水果或蔬菜的数量。此后,他们的水果/蔬菜将被添加到购物车中,乘以他们输入的数量。 完成此操作后,应打印购物车,以便用户可以看到购物车中的商品。

我的第一个问题是,我不知道如何让水果/蔬菜的数量出现在我的列表中,当用户输入水果/蔬菜时,购物车会打印,但打印为空。

第二个问题是,在打印购物车后,我希望再次出现相同的问题(询问用户想要什么),而不必每次都重新输入。

#This has the inventory as a list
inventory_list = [apple, asparagus, avocado, banana, bell_pepper, broccoli, cabbage, cantaloupe, cauliflower, cilantro, corn, cucumber, garlic, green_onion, lemon, lettuce, lime, mango, nectarine, onions, orange, peach, pear, pineapple, plum, tomatoes, watermelon]

#Shopping cart
cart = []

#Welcome message for user and print
print("Hello and welcome to Rica's Produce Market!!  :)")
print("") #separates text, so it's not bunched up

#This should list the fruits and vegetables for the customer to view, should not do anything to code
for_cust_view = print("FRUITS & VEGGIES: Apple | Asparagus | Avocado | Banana |Bell Pepper | Broccoli | Cabbage |Cantaloupe | Cauliflower | Cilantro | Corn | Cucumber | Garlic | Green Onion | Lemon | Lettuce | Lime | Mango | Nectarine | Onion | Orange | Peach | Pear | Pineapple | Plum | Tomato | Watermelon")
print(" ~   ~   ~   ~   ~") #separates text, so it's not bunched up

#This should ask the user what they want to add to their cart from certain categories
type_of_item = str(input("Please choose whichever fruit or vegetable you want to add to your cart from the list above (in singular form): "))

#This should ask the user for the quantity of that item
num_of_item = int(input("Alright, now how many " + type_of_item +"s would you like? "))

#If customer response is equal to an item in cart it should add it to cart, if not - tell customer "not available"

def add_cart():
cart.append(num_of_item)

add_cart

#Prints the cart
print(cart)
python python-3.x
1个回答
0
投票

你可以这样做:



#Shopping cart
cart = []

#Welcome message for user and print
print("Hello and welcome to Rica's Produce Market!!  :)")
print("") #separates text, so it's not bunched up

#This should list the fruits and vegetables for the customer to view, should not do anything to code
for_cust_view = print("FRUITS & VEGGIES: Apple | Asparagus | Avocado | Banana |Bell Pepper | Broccoli | Cabbage |Cantaloupe | Cauliflower | Cilantro | Corn | Cucumber | Garlic | Green Onion | Lemon | Lettuce | Lime | Mango | Nectarine | Onion | Orange | Peach | Pear | Pineapple | Plum | Tomato | Watermelon")
print(" ~   ~   ~   ~   ~") #separates text, so it's not bunched up

#This should ask the user what they want to add to their cart from certain categories
type_of_item = str(input("Please choose whichever fruit or vegetable you want to add to your cart from the list above (in singular form): "))

#This should ask the user for the quantity of that item
num_of_item = int(input("Alright, now how many " + type_of_item +"s would you like? "))

#If customer response is equal to an item in cart it should add it to cart, if not - tell customer "not available"

cart.append({"type":type_of_item, "quantity": num_of_item})



#Prints the cart
print(cart)

您可以在列表中为每个项目添加字典及其数量。

© www.soinside.com 2019 - 2024. All rights reserved.