【问题标题】:How to fix this (python)如何解决这个问题(python)
【发布时间】: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


【解决方案1】:

if budget >= 0 更改为while budget >= 0 是正确的想法,您只需要将用户输入请求也移到while 循环中。这样它会要求输入,检查商店中的物品,然后如果budget >= 0 它会再次这样做,从请求更多输入开始。

#!/usr/bin/env python
print("Welcome to the store!")

setup_price_list() #pseudocode

budget = 20

while budget >= 0:
    buy = input("What would you like to buy?")
    if buy == 'apple':
        print("It cost 3 dollars")
        budget -= 3
        inventory.append('apple')
        print("Your inventory is below")
        print(inventory)

    #the rest of the if statements

    print("You bought", buy)
    print("Your inventory is below")
    print(inventory)

一旦你完成了这项工作,我建议你看一下称为字典的 Python 数据结构。它可以使这样的代码更简单,例如:

print("Welcome to the store!")
price_list = {'apple':3, 'orange':2, 'grapefruit':5, 'bomb':15, 'gun':10}
print("You can buy:")
for key in price_list:
    item = key
    price = price_list[key]
    print(item, price)

budget = 20
while budget > 0:
    buy = raw_input("What do you want to buy?")
    price = price_list[buy] #You will get an error if you give it a key it doesn't have
    budget -= price
    print("You bought: ", buy)
    print("It costed: $", price)

哦,您可能想在购买之前添加一张支票,看看您是否真的有足够的钱来购买该物品,否则只要您不欠债,您仍然可以购买任何东西,我会留给你自己弄清楚:)

【讨论】:

  • 您的示例中存在一个错误,即用户可以在没有足够预算的情况下购买某些东西。您需要一个“if”语句来检查他们是否有足够的预算来购买该项目。
  • 引用:“哦,你可能想在购买之前添加一张支票,看看你是否真的有足够的钱来购买该物品,否则你仍然可以购买任何东西,只要你没有欠债,我会留给你去弄清楚:)”现在不想让他太容易......
猜你喜欢
  • 2014-10-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-20
相关资源
最近更新 更多