【问题标题】:My code isnt writing to a list. The details are not displayed properly at the end我的代码没有写入列表。细节最后没有正确显示
【发布时间】:2016-01-30 19:49:55
【问题描述】:

代码应该是这样工作的:提示用户选择一种饮料,然后是菜肴,然后是菜肴。在此之后,程序显示用户想要的顺序。

问题是,当客户输入他们的详细信息时,最后没有正确显示详细信息。

代码应该做什么显示在代码下方。

print("Welcome to Hungary house.")
addDrink = input("Do you want a drink?\n").lower()
drinkChoice = "None"
dish = ""
order = []

if addDrink == "yes":
    print ("What drink would you prefer?")
    print ("1: Fanta")
    print ("2: Coke")
    print ("3: Pepsi")
    print ("4: Sprite")
    drinkChoice = input("please select a drink from the options above\n")

    if drinkChoice == "1" or drinkChoice == "fanta":
        drinkChoice = "Fanta"
        order.insert(0,drinkChoice)
    if drinkChoice == "2" or drinkChoice == "coke":
        drinkChoice = "Coke"
        order.insert(0,drinkChoice)
    if drinkChoice == "3" or drinkChoice == "pepsi":
        drinkChoice = "Pepsi"
        order.insert(0,drinkChoice)
    if drinkChoice == "4" or drinkChoice == "sprite":
        drinkChoice = "Sprite"
        order.insert(0,drinkChoice)

print ("you have chosen this drink: " + drinkChoice)

foodGroup = input("Do you want Indian or Chinese food?\n").lower()
if foodGroup == "indian" or foodGroup == "Indian":
    dish = input("Do you want Curry or onion bhaji?\n").lower()
    if dish == "Curry":
        order.insert(1,dish)
        dish = input("Do you want Rice or Naan?\n").lower()
        if dish == "Rice":
            order.insert(2,dish) 
        if dish == "Naan":
            order.insert(2,dish)

    if dish == "onion bhaji":
        order.insert(1,dish)
        dish = input("Do you want Chilli or Peppers?\n").lower()
        if dish == "Chilli":
            order.insert(2,dish)
        if dish == "Peppers":
            order.insert(2,dish)

if foodGroup == "chinese" or foodGroup == "Chinese":
    dish = input("Do you want Chicken Wings or Noodles?\n").lower()
    if dish == "Chicken Wings":
        order.insert(1,dish)
        dish = input("Do you want Chips or Red peppers?\n").lower()
        if dish == "Chips":
            order.insert(2,dish) 
        if dish == "Red peppers":
            order.insert(2,dish)

    if dish == "Noodles":
        order.insert(1,dish)
        dish = input("Do you want Meatballs or Chicken?\n").lower()
        if dish == "Meatballs":
            order.insert(2,dish)
        if dish == "Chicken":
            order.insert(2,dish)

print ("You have ordered the following",order,"You are order number 294")

这段代码的逻辑应该是这样的:

Welcome to Hungary house.
Do you want a drink?
If no, Move on to the next question
If yes, ask what drink the customer wants.
(Coke, Fanta, lemonade are the options)
If coke is chosen, set coke as drink
If Fanta is chosen, set Fanta as drink
If Lemonade is chosen, set Lemonade as drink

你想要印度菜还是中国菜?

If Indian food is entered ask the following questions
Do you want Curry or onion bhaji?
If curry is selected ask the following questions.
What toppings you want on top of your Curry?
Rice 
Or 
Naan
If user enters rice, add rice on to the curry.
If user enters Naan, add naan on to the curry.
If user enters onion ask the following questions.
What toppings do you want on top of your onion bhaji?
Chilli source
Or 
Naan 
If user enters Chilli source, add chilli source on to the onion bhaji.
If user enters Naan, add Naan on to the onion bhaji.

.

If Chinese food is entered ask the following questions.
Do you want Chicken wings or Noodles?
If Chicken wings is chosen ask,
Do you want with your Chicken wings?
Green Peppers
Or
Red Peppers
If Green pepper is chosen add that to the Green Peppers to the Chicken wings
If Red Peppers are chosen add that to the Chicken wings.
If the user enters Noodles ask the following questions.
Choose the toppings you want with Noodles.
Meatballs 
Or
Chicken 
If user enters Meatballs, add Meatballs to the Noodles.
If user enters Chicken, add Chicken to the Noodles. 

在此之后,显示用户想要的订单,告诉客户他们的订单将在接下来的 10-50 分钟内到达。如果他们想取消,请告诉他们联系这个号码“077 3475 XXXXXX”。

【问题讨论】:

  • 每次使用“or”时,都可以使用“in”:如果drinkChoice == "2"或者drinkChoice == "coke"也可以写成:如果drinkChoice in ["2", “可乐”]
  • 这个测试永远不会起作用:dish = input("Do you want Curry or onion bhaji?\n").lower() ....if disc == "Curry" "Curry" cannot当您将用户输入转换为小写时,请使用大写 C。
  • 有趣的是,您有设置 yes 和 no 的想法
  • @Luis Sieira 那我该怎么办?
  • @Apero 你能回答这个问题并给我一个例子吗

标签: python list if-statement input


【解决方案1】:
# Let's put the initialization stuff up front:
Order = [ 'No drink', 'No food' ]
Yesses = [ 'y', 'yes', 'yep', 'si', 'hai', 'sure', 'haan', 'why not?', 'ja', 'da' ]
DRINK = 0
FOOD = 1

print("Welcome to Hungary house!")

print("")
print("First, let me get your drink order, if any.")

answer = input("Would you like something to drink? ").lower()

if answer in Yesses:
    print("What drink would you prefer?")
    print ("1: Fanta")
    print ("2: Coke")
    print ("3: Pepsi")
    print ("4: Sprite")

    choice = input("Please enter a drink from the menu above\n").lower()

    if choice == "1" or choice == "fanta":
        Order[DRINK] = "Fanta"
    if choice == "2" or choice == "coke":
        Order[DRINK] = "Coke"
    if choice == "3" or choice == "pepsi":
        Order[DRINK] = "Pepsi"
    if choice == "4" or choice == "sprite":
        Order[DRINK] = "Sprite"

print ("You have chosen: " + Order[DRINK])

print("")
print("Now, let me get your food order, if any.")

answer = input("Do you want any food (Y/N)? ").lower()
if answer in Yesses:
    answer = input("Do you want Indian or Chinese food?\n").lower()

    if answer == "indian":
        answer = input("Do you want Curry or Onion Bhaji?\n").lower()

        if answer == "curry":
            Order[FOOD] = "Curry"

            answer = input("With your curry, do you want Rice or Naan?\n").lower()
            if answer == "rice":
                Order.append("- with rice")
            if answer == "naan":
                Order.append("- with naan")

        if answer == "onion bhaji" or answer == "bhaji":
            Order[FOOD] = "Onion Bhaji"

            answer = input("With your bhaji, do you want Chili or Peppers?\n").lower()
            if answer == "chili":
                Order.append("- with chili")
            if answer == "peppers":
                Order.append("- with peppers")
    if answer == "chinese":
        answer = input("Do you want Chicken Wings or Noodles?\n").lower()

        if answer == "chicken wings" or answer == "wings":
            Order[FOOD] = "Chicken Wings"

            answer = input("With your wings, do you want Chips or Red Peppers?\n").lower()
            if answer == "chips":
                Order.append("- with Chips")
            if answer == "red peppers" or answer == "peppers":
                Order.append("- with Red Peppers")

        if answer == "noodles":
            Order[FOOD] = "Noodles"

            answer = input("With your noodles, do you want Meatballs or Chicken?\n").lower()
            if answer == "meatballs":
                Order.append("- with meatballs")
            if answer == "chicken":
                Order.append("- with chicken")

try:
    if Order[2]:
        print("You have ordered the following. Your order number is 294")
        print("")
        print("    ", Order[DRINK])
        print("    ", Order[FOOD])
except:
    pass

try:
    if Order[2]:
        print("    ",Order[2])
except:
    print("")
    print("No food or drink?! Get out!")

try:
    if Order[2]:
        print("""
        Your order should arrive within 10-50 minutes. If you wish to cancel,
        please contact us at 077 3475 8675309. Thank you for your patronage!
        """)
except:
    pass

【讨论】:

  • 你能验证一下吗
  • 所以如果我输入错误它会一直循环直到我给出一个有效的答案? @R 卡普
  • @Rdowg 如果您输入错误,则假定以上都不是。但是,我可以让它继续循环,直到你给出一个有效的答案。
  • 如果你能做到,让它继续循环,请@R.Kap
【解决方案2】:

不要做list.insert(1, dish)list.append(dish)。由于您没有在列表末尾之前插入项目.append() 对您来说效果很好。示例代码:

if drinkChoice == "2" or drinkChoice == "coke":
    order.append("Coke")

另外,用Coke 的内容创建一个字符串然后将该字符串添加到列表中是没有意义的,您可以将字符串直接添加到列表中,如上所示。

另一个例子:

if dish == "onion bhaji":
    order.append(dish)
    dish = input("Do you want Chilli or Peppers?\n").lower()
    if dish == "chilli":
        order.append(dish)
    if dish == "peppers":
        order.append(dish)

希望这会有所帮助。

【讨论】:

  • @Alfabravo 请给我看另一个例子。
  • @Rdowg 我刚刚添加了另一个示例。
  • 在您的第二个示例中,Chilli 不会写入列表
  • @Rdowg 可能是因为您正在检查它是否与Chilli 匹配,但是您已经将输入转换为带有.lower() 的小写字符
  • @Jasan Heddle 那我该怎么办?
猜你喜欢
  • 2018-10-29
  • 1970-01-01
  • 1970-01-01
  • 2019-05-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-17
  • 2010-12-09
相关资源
最近更新 更多