【发布时间】:2011-03-29 17:16:52
【问题描述】:
我正在学习 Python,并且正在尝试制作购物车(用于练习)。但我有点卡在这里:
# Vars
budget = 200; # Budget
total = 0; # Total price
prices = { # Price list
"cola":22,
"chips":18,
"headset":800,
"pencil":5
}
cart = [] # Shopping cart
while True:
cmd = raw_input("Enter command: ");
if cmd == "additem":
在 while 循环(特别是“if cmd ==”additem”)中,我希望用户输入商品的名称(来自价格字典),然后将其添加到购物车中。但是,我不是确定如何去做。
【问题讨论】:
-
对我来说听起来像是一个家庭作业问题:),你到底卡在哪里了?你不知道如何从用户那里得到输入吗?您是否希望通过 shell 从命令行执行此操作?
标签: python dictionary shopping-cart