向尚未包含用户输入值的列表中追加和插入用户输入值

2024-05-04 17:24:46 发布

您现在位置:Python中文网/ 问答频道 /正文

drinksList = ["Fanta", "CocaCoal", "7up"]

userDrink = input("What soft drink are you looking for?")
if userDrink in drinksList:
    print(userDrink,"is in stock!")
elif userDrink not in drinksList:
    print("Sorry,", userDrink,"is currently not is stock.")

From here I would like to ask the user whether or not they would like to order the named soda in. If they answer yes I want to append or insert their named soda into my list.

order = input("Would you like us to order some for you?")
if order == "yes" or order == "Yes":
    drinksList.append("userDrink")
    print("Ok, we have added", userDrink,"to our stock!")
    print(drinksList)

I would also want the code to end if the user does not want to order their drink. But at the end display the message "Thank-you for coming!". I only want this message to appear through this path and not at the end of the code, which keeps happening.

else:
    print("Ok, thank-you for coming!")
    print("Ok, we have added", userDrink," to our stock!")

Here it does not print what the user has inputted but instead prints the word userDrink.


1条回答
网友
1楼 · 发布于 2024-05-04 17:24:46

不完全确定你想要什么,但可能是这样的:

drinksList = ["Fanta", "CocaCoal", "7up"]

userDrink = input("What soft drink are you looking for?")
if userDrink in drinksList:
    print(userDrink,"is in stock!")
else:
    print("Sorry,", userDrink,"is currently not is stock.")
    order = input("Would you like us to order some for you?")
    if order.lower() == 'yes':
       drinksList.append(userDrink)
       print("Ok, we have added", userDrink,"to our stock!")
       print(drinksList)
    else:
       print('Thank you for coming')

相关问题 更多 >