2012-02-09 21 views
0

파이썬으로 약간의 응용 프로그램을 만들고 있지만 다음에 무엇을해야할지 전혀 모릅니다.이 문제를 해결하는 방법 (파이썬)

#!/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) 

내가 내 예산을 reahed 때까지 그렇게 난 후, 한 가지를 추가 할 다른 일을 추가 할 수 있습니다 만들고 싶어,하지만 난 잠시 문을 사용하는 경우, 그냥 내가 사는 일을 추가 유지 ! 도와주세요!

+3

이 숙제입니까? 미안하지만 지금 당장은 숙제의 냄새가 난다. ... – blahman

+3

아, 숙제인지 알아낼 때까지 내 대답을 지우겠다. 조금 너무 쉬웠다. –

+0

@ mathematical.coffee - upvote ... 당신은 그에게 힌트를 제공해야합니다! :) 아마도 나는 숙제로 다시 태그를 붙였습니다. 그렇지 않더라도, 그것은 그 구경입니다. – prelic

답변

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) 

아, 그리고 당신은 당신이 실제로 충분한 돈이 당신이 그것을 구입하기 전에 항목을 구입하는 왼쪽 여부를 확인하기 위해 검사에 추가 할 수 있습니다 : 그것은 예를 들어, 훨씬 더 간단 같은 코드를 만들 수 있습니다 당신이 빚을지고 있지 않는 한 무엇이든을 사십시오, 나는 당신을 알아낼 것입니다 :

+0

예제에 버그가 있습니다. 즉, 사용자가 비용을 지불 할 수있는 충분한 예산을 확보하지 못한 상태에서 구매할 수있는 버그가 있습니다. 아이템에 충분한 예산이 있는지 확인하려면 'if'문이 필요합니다. –

+0

인용하기 : "아, 그리고 물건을 사기 전에 물건을 사기에 충분한 돈이 있는지를 확인하기 위해 수표를 추가하고 싶을 것입니다. 그렇지 않으면 빚이없는 한 무엇이든 살 수 있습니다. 알아낼 수 있도록 남겨주세요 :) " 지금은 그를 너무 쉽게 만들고 싶지 않습니다 ... – SudoNhim

관련 문제