三明治机(Python)

三明治机

编写一个程序,向用户询问他们的三明治偏好。程序应使PyInputPlus来确保有效的输入
面包类型使用inputMenu():wheat、white、sourdough
对蛋白质类型使用inputMenu():chicken、turkey、ham或tofu
用inputYesNo()询问他们是否要cheese
如果需要cheese,请使用inputMenu()询问cheese类型:cheddar、Swiss或mozzarella
用inputYesNo询问他们是否需要mayo、mustard、lettuce或tomato
用inputInt()询问他们想要多少个三明治。确保这个数字为1或更大
列出每个选项的的价格,并在用户输入选择后让程序显示总费用

核心代码

def pyIP(text, choice, yn):
    if text:
        print(text)
    if yn == True:
        return pyip.inputYesNo(prompt='')
    elif yn == False:
        return pyip.inputMenu(
            [k + ' ' + v for k, v in menu[choice].items()],
            numbered=True, prompt='') # numbered = True会显示选项1、2、3...,并且可以通过输入1、2、3来进行选择
    elif yn == 'end':
        return pyip.inputInt(min=1, prompt='') # 至少得要一个

for i in range(len(menuText)):
    if i <= 1:
        order.append(pyIP(menuText[i], i, False))
    elif i in range(2, 5):
        if pyIP(menuText[i], i, True) == 'yes':
            order.append(pyIP('', i, False))
    elif i == 5:
        totalSandwiches = pyIP(menuText[i], i, 'end')

for i in [re.sub(r'[^0-9.]', '', i) for i in order]: # 匹配除了0-9和.之外的所有字段,并且将这些字段替换为空
    totalCost += float(i) * totalSandwiches

完整项目地址:
[1]: https://github.com/linkneo-cmd/Automatetheboring/blob/main/chapter8/sandwich.py

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值