Модераторы: Daevaorn
  

Поиск:

Ответ в темуСоздание новой темы Создание опроса
> задача с codecademy, не могу понять что от меня хотят) 
:(
    Опции темы
Gluck1986
Дата 16.4.2013, 00:03 (ссылка) | (нет голосов) Загрузка ... Загрузка ... Быстрая цитата Цитата


Новичок



Профиль
Группа: Участник
Сообщений: 37
Регистрация: 18.2.2008

Репутация: нет
Всего: нет



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

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

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.

Что я делаю не так(
PM MAIL   Вверх
Crafty
Дата 16.4.2013, 07:39 (ссылка) | (нет голосов) Загрузка ... Загрузка ... Быстрая цитата Цитата


Опытный
**


Профиль
Группа: Участник
Сообщений: 319
Регистрация: 3.11.2008

Репутация: нет
Всего: 14



Внимательней условие читай 
Цитата

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)

PM MAIL   Вверх
Gluck1986
Дата 16.4.2013, 10:52 (ссылка) | (нет голосов) Загрузка ... Загрузка ... Быстрая цитата Цитата


Новичок



Профиль
Группа: Участник
Сообщений: 37
Регистрация: 18.2.2008

Репутация: нет
Всего: нет



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

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.


то ли лыжи не едут то ли я....
PM MAIL   Вверх
Crafty
Дата 16.4.2013, 11:18 (ссылка) | (нет голосов) Загрузка ... Загрузка ... Быстрая цитата Цитата


Опытный
**


Профиль
Группа: Участник
Сообщений: 319
Регистрация: 3.11.2008

Репутация: нет
Всего: 14



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

if stock[item] > 0:

PM MAIL   Вверх
  
Ответ в темуСоздание новой темы Создание опроса
1 Пользователей читают эту тему (1 Гостей и 0 Скрытых Пользователей)
0 Пользователей:
« Предыдущая тема | Python: Общие вопросы | Следующая тема »


 




[ Время генерации скрипта: 0.0916 ]   [ Использовано запросов: 21 ]   [ GZIP включён ]


Реклама на сайте     Информационное спонсорство

 
По вопросам размещения рекламы пишите на vladimir(sobaka)vingrad.ru
Отказ от ответственности     Powered by Invision Power Board(R) 1.3 © 2003  IPS, Inc.