开发者社区> 问答> 正文

如何简化程序PYTHON

我有一个任务,我必须做一个组合菜单。基本上我必须符合这些标准。 迭代1 向用户索要一种三明治(鸡肉5.25美元,牛肉6.25美元,豆腐5.75美元)。 让程序输出用户三明治选择,以帮助验证程序是否正确工作。 迭代2 将以下特性添加到第一次迭代中。 询问用户是否需要饮料(是,不是)。 如果他们说是,问他们想要多大的饮料: 小:$ 1.00 介质:$ 1.75 大:2.25美元 有程序输出用户饮料大小选择,或缺少选择,以帮助验证程序是否正确工作。 让程序输出到目前为止的总成本。 我刚接触Python,我真的不知道如何简单地使用这个程序。

if sandwich=='Chicken':
    print "Okay! So a chicken sandwich, your total cost is $5.25"
if sandwich=='Beef':
    print 'Okay! So a beef sandwich, your total cost is $6.25'
if sandwich=='Tofu':
    print 'Okay! So a tofu sandwich, your total cost is $5.75'


beverage= raw_input("Would you like a beverage? Yes or No? ")
if beverage=='Yes':
    size_beverage= raw_input("What size? Small, Medium, or Large? ")


    if size_beverage=='Small': 
        if sandwich=='Chicken':
            print 'Your total is $6.25'
            print 'Okay! so a', sandwich,'sandwich and a small beverage'
        if sandwich=='Beef':
            print 'Your total is $7.25'
            print 'Okay! so a',sandwich,'sandwich and a small beverage'
        if sandwich=='Tofu':
            print 'Your total is $6.75'
            print 'Okay! so a', sandwich,'sandwich and a small beverage'

    if size_beverage=='Medium': 
        if sandwich=='Chicken':
            print 'Your total is $7'
            print 'Okay! so a', sandwich,'sandwich and a medium beverage'
        if sandwich=='Beef':
            print 'Your total is $8'
            print 'Okay! so a',sandwich,'sandwich and a medium beverage'
        if sandwich=='Tofu':
            print 'Your total is $7.50'
            print 'Okay! so a', sandwich,'sandwich and a medium beverage'

    if size_beverage=='Large':
        if sandwich=='Chicken':
            print 'Your total is $7.50'
            print 'Okay! so a', sandwich,'sandwich and a large beverage'
        if sandwich=='Beef':
            print 'Your total is $8.50'
            print 'Okay! so a',sandwich,'sandwich and a large beverage'
        if sandwich=='Tofu':
            print 'Your total is $8'
            print 'Okay! so a', sandwich,'sandwich and a large beverage'

if beverage=='No':
    print 'Okay! so just a', sandwich,"sandwich?"```




问题来源StackOverflow 地址:/questions/59383978/how-to-simplify-program-python

展开
收起
kun坤 2019-12-26 15:43:31 739 0
1 条回答
写回答
取消 提交回答
  • 代码优化可以根据or and 这样并列关系避免过多的判断哦

    2020-03-25 17:08:54
    赞同 展开评论 打赏
问答分类:
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
From Python Scikit-Learn to Sc 立即下载
Data Pre-Processing in Python: 立即下载
双剑合璧-Python和大数据计算平台的结合 立即下载