【发布时间】:2012-02-09 02:29:25
【问题描述】:
我正在用 python 做一个小应用程序,但我不知道下一步该做什么。
#!/usr/bin/env python
print("Welcome to the store!")
price_list = [['apple', 'orange', 'grapefruit', 'bomb', 'gun'], [3, 2, 5, 15, 10]]
inventory = []
print("You can buy the following things")
print(price_list[0][0])
print(price_list[1][0], 'dollars')
print(price_list[0][1])
print(price_list[1][1], 'dollars')
print(price_list[0][2])
print(price_list[1][2], 'dollars')
print(price_list[0][3])
print(price_list[1][3], 'dollars')
print(price_list[0][4])
print(price_list[1][4], 'dollars')
budget = 20
buy = input("What would you like to buy? Type in one of the previous options to buy something You only have 20 dollars to spend though! ")
print("You bought", buy)
if budget >= 0:
if buy == 'apple':
print("It cost 3 dollars")
budget -= 3
inventory.append('apple')
print("Your inventory is below")
print(inventory)
if buy == 'orange':
print("It cost 2 dollars")
budget -= 2
inventory.append('orange')
print("Your inventory is below")
print(inventory)
if buy == 'grapefruit':
print("It cost 5 dollars")
budget -= 5
inventory.append('grapefruit')
print("Your inventory is below")
print(inventory)
if buy == 'bomb':
print("It cost 15 dollars")
budget -= 15
inventory.append('bomb')
print("Your inventory is below")
print(inventory)
if buy == 'gun':
print("It cost 10 dollars")
budget -= 10
inventory.append('gun')
print("Your inventory is below")
print(inventory)
我想做它,这样我就可以添加一个东西,然后能够添加另一个东西,直到我达到我的预算,但如果我使用 while 语句,它只会不断添加我买的东西!请帮忙!
【问题讨论】:
-
这是作业吗?如果不是,我很抱歉,但现在它有家庭作业的味道...
-
哦,我要删除我的答案,直到我知道它是否是家庭作业 - 否则它会有点太容易了。
-
@mathematical.coffee - 对此投赞成票...不过你应该给他一个提示! :) 我重新标记为家庭作业,可能是这样,即使不是,它也是那样的水准。
-
我很乐意提供帮助,但如果是家庭作业,我不会说“这是有效的重写代码”,这就是我目前的回答。
标签: python python-3.x