несколько операторов if выводятся с namedtuple - PullRequest
1 голос
/ 01 мая 2020

Я пытаюсь расположить операторы if, но все же есть ошибка, из-за которой мой код не продолжает l oop. Может кто-нибудь помочь переставить логи c операторов 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

1 Ответ

0 голосов
/ 01 мая 2020

Вы были близки. Теперь он зацикливается, но вам нужно исправить свой индекс. Индекс списка начинается с 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
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...