Python中的购物清单

2024-04-24 23:27:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在通过编写小程序来学习Python(以及一般的编程)。下面是一个基本的购物计划,它将返回一个基于所选食物的购物项目列表。

我想改进它,允许用户一次选择几种食物(例如,用户输入是“1,2,3”),并返回基于此的配料列表。

我应该采取什么样的方法?我使用的是Python2.7,下面是我的现有代码:

mm_soup = ['minced meat', 'potatoes', 'frozen vegetable']
sunday_soup = ['chicken with bones', 'noodles', 'soup vegetable']
gulas = ['pork meat', 'food cream', 'potatoes', 'onion', 'frozen peas']

print "What would you like to cook on weekend?"
print "Here are the options:"
print "1. Minced Meat Soup"
print "2. Sunday Soup"
print "3. Gulas"

choose = raw_input("> ")

if choose == '1':
    print "Buy", ", ".join(mm_soup) + "." 

elif choose == '2':
    print "Buy", ", ".join(sunday_soup) + "."

elif choose == '3':
    print "Buy", ", ".join(gulas) + "."

else:
    print "Hmmm. No such food on the list."

Tags: 用户列表buy购物食物mmprintjoin