Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > Python: Общие вопросы > задача с codecademy


Автор: Gluck1986 16.4.2013, 00:03
Доброго. Вот решил изучить Питон, благо указанный ресурс в этом не плохо помогает, прошел больше половины заданий,  и тут вперся в стенку( и вся проблема в том что я не понимаю что от меня хотят.. в английском я не особо силен..

Собственно текст задания:
Цитата

Making a Purchase
Good! Now you're going to need to know how much you’re paying for all of the items on your grocery list.

Remember how to compute a rolling sum? If not, here's a reminder: total += price is the same as total = total + price. Evaluating the right hand side grabs the values of those variables, adds those values and then stores it back into the variable total.

INSTRUCTIONS
Write a function compute_bill that takes a parameter food as input and computes your bill by looping through your food list and summing the costs of each item in the list.
For now, go ahead and ignore whether or not the item you're billing for is in stock.


Hint
First you will need a variable total with an initial value of zero.
While looping though your list, add the price of every object in the list into total.


а вот что я делаю

Код

groceries = ["banana", "orange", "apple"]

stock = { "banana": 6,
    "apple": 0,
    "orange": 32,
    "pear": 15
}
    
prices = { "banana": 4,
    "apple": 2,
    "orange": 1.5,
    "pear": 3
}


def compute_bill(food):
    total=0
    for item in food:
        if stock[item]<>0:
            total+=prices[item]
    return total
    


print compute_bill(groceries)


в итоге получаю рабочий код и верный ответ вроде бы), но ошибку по заданию, вида:
Цитата

Oops, try again! You code does not seem to work when [u'apple'] is used as input--it returns 0 instead of 2.

Что я делаю не так(

Автор: Crafty 16.4.2013, 07:39
Внимательней условие читай 
Цитата

For now, go ahead and ignore whether or not the item you're billing for is in stock.

Код


groceries = ["banana", "orange", "apple"]
stock = { "banana": 6,
    "apple": 0,
    "orange": 32,
    "pear": 15
}
    
prices = { "banana": 4,
    "apple": 2,
    "orange": 1.5,
    "pear": 3
}
def compute_bill(food):
    total=0
    for item in food:
        total+=prices[item]
    return total
    
print compute_bill(groceries)

Автор: Gluck1986 16.4.2013, 10:52
О спасибо, вот я торопыга. Но проблемы продолжаются))) следующие задание. та же колбаса
Цитата

Stocking Out
Now you need your compute_bill function to take the stock/inventory of a particular item into account when computing the cost.

Ultimately, if an item isn't in stock, then it shouldn't be included in the total. You can't buy or sell what you don't have!

INSTRUCTIONS
Do the following for your compute_bill function:
Let your function take a list of groceries as input.
Do not add the price of an item into your list if it is out of stock.
After you buy an item, subtract one from its stock.
If an item is in your list multiple times you must repeat this process multiple times.


Код

groceries = ["banana", "orange", "apple"]
stock = { "banana": 6,
    "apple": 0,
    "orange": 32,
    "pear": 15
}
    
prices = { "banana": 4,
    "apple": 2,
    "orange": 1.5,
    "pear": 3
}
def compute_bill(food):
    total=0
    for item in food:
        if stock[item]<>0:
            total+=prices[item]
            stock[item]-=1
    return total
    
print compute_bill(groceries)



Цитата

Oops, try again! Your total is too low. Make sure you are getting every item in the list.


то ли лыжи не едут то ли я....

Автор: Crafty 16.4.2013, 11:18
Все нормально, удали только 21 строчку, единственно если ближе к условию то я бы написал так
Код

if stock[item] > 0:

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)