多个if语句输出与namedtuple

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

我正在尝试排列if语句,但是仍然出现错误,使我的代码无法继续循环。任何人都可以帮助重新安排if语句的逻辑。预先谢谢你

import re
from collections import namedtuple

MyMenu = namedtuple('MyMenu', ['index','dish','price'])

_menu = []
_menu.append(MyMenu(1, 'Plate A', '($30.50)'))
_menu.append(MyMenu(2, 'Plate B', '($26.50)'))
_menu.append(MyMenu(3, 'Plate C', '($30.50)'))
_menu.append(MyMenu(4, 'Plate D', '($35.50)'))
_menu.append(MyMenu(5, 'Plate E', '($32.50)'))
_menu.append(MyMenu(6, 'Plate F', '($29.50)'))
_menu.append(MyMenu(7, 'Plate G', '($30.50)'))
_menu.append(MyMenu(8, 'Plate H', '($21.50)'))
total_bill = 0.0
order_summary = []
print("Welcome to Our Restaurant!\n")
print("Here is our meue:")
for entry in _menu:
    index = str(getattr(entry,'index')).ljust(8)
    descr = getattr(entry,'dish').ljust(25)
    price = getattr(entry,'price').ljust(7)
    print ('{0}{1}{2}'.format(index,descr,price))
print("\nWhat would you like to order?")
while True:
    order = input('')
    if 9 > int(order) > 0:
        item = _menu.__getitem__(int(order)). dish
        price = _menu.__getitem__(int(order)).price
        a= float("".join(re.findall("\d+\.\d+", _menu.__getitem__(int(order)).price)))
        print("You've selected {}! That would be ${:.2f}".format(item, a))
        total_bill += a
        order_summary.append(item)
        print("Would you like to add more items? (y/n)")
        #continue
        if order == "n":
            print("###################################################")
            print("Your order summary: {", ', '.join(order_summary), '}')
            print("Total Price: $", total_bill)
            break
        elif order == "y":
            item = _menu.__getitem__(int(order)).dish
            price = _menu.__getitem__(int(order)).price
            a = float("".join(re.findall("\d+\.\d+", _menu.__getitem__(int(order)).price)))
            print("You've selected {}! That would be ${:.2f}".format(item, a))
            total_bill += a
            order_summary.append(item)
            continue
   else:
       print("Selection Not Found!")
       print("Would you like to add more items? (y/n)")
       if order == "n":
           print("No items ordered at Carmine's :(")
           break
       elif order == "y":
           item = _menu.__getitem__(int(order)).dish
           price = _menu.__getitem__(int(order)).price
           a = float("".join(re.findall("\d+\.\d+", _menu.__getitem__(int(order)).price)))
           print("You've selected {}! That would be ${:.2f}".format(item, a))
           total_bill += a
           order_summary.append(item)
           continue
python namedtuple
1个回答
0
投票

你很近。现在循环了,但是您需要修复索引。列表索引以0开头,而不是1。因此,如果我选择1,我得到的是板B,而不是板A。

import re
from collections import namedtuple

MyMenu = namedtuple('MyMenu', ['index','dish','price'])

_menu = []
_menu.append(MyMenu(1, 'Plate A', '($30.50)'))
_menu.append(MyMenu(2, 'Plate B', '($26.50)'))
_menu.append(MyMenu(3, 'Plate C', '($30.50)'))
_menu.append(MyMenu(4, 'Plate D', '($35.50)'))
_menu.append(MyMenu(5, 'Plate E', '($32.50)'))
_menu.append(MyMenu(6, 'Plate F', '($29.50)'))
_menu.append(MyMenu(7, 'Plate G', '($30.50)'))
_menu.append(MyMenu(8, 'Plate H', '($21.50)'))
total_bill = 0.0
order_summary = []
print("Welcome to Our Restaurant!\n")
print("Here is our meue:")
for entry in _menu:
    index = str(getattr(entry,'index')).ljust(8)
    descr = getattr(entry,'dish').ljust(25)
    price = getattr(entry,'price').ljust(7)
    print ('{0}{1}{2}'.format(index,descr,price))
while True:
    order = input("\nWhat would you like to order?")
    if 9 > int(order) > 0:
        item = _menu.__getitem__(int(order)).dish
        price = _menu.__getitem__(int(order)).price
        a = float("".join(re.findall("\d+\.\d+", _menu.__getitem__(int(order)).price)))
        print("You've selected {}! That would be ${:.2f}".format(item, a))
        total_bill += a
        order_summary.append(item)
        userAnswer = input("Would you like to add more items? (y/n)")
        if "n" in userAnswer:
            print("###################################################")
            print("Your order summary: {", ', '.join(order_summary), '}')
            print("Total Price: $", total_bill)
            break
        elif "y" in userAnswer:
            item = _menu.__getitem__(int(order)).dish
            price = _menu.__getitem__(int(order)).price
            a = float("".join(re.findall("\d+\.\d+", _menu.__getitem__(int(order)).price)))
            print("You've selected {}! That would be ${:.2f}".format(item, a))
            total_bill += a
            order_summary.append(item)
            continue
    else:
       print("Selection Not Found!")
       print("Would you like to add more items? (y/n)")
       if order == "n":
           print("No items ordered at Carmine's :(")
           break
       elif order == "y":
           item = _menu.__getitem__(int(order)).dish
           price = _menu.__getitem__(int(order)).price
           a = float("".join(re.findall("\d+\.\d+", _menu.__getitem__(int(order)).price)))
           print("You've selected {}! That would be ${:.2f}".format(item, a))
           total_bill += a
           order_summary.append(item)
           continue
© www.soinside.com 2019 - 2024. All rights reserved.