11. making a purchase


#1

I don't mean to hijack this conversation, but I would like to see if you could explain one little thing for me.

With the code

def compute_bill(food):
    total = 0
    for items in food:
        total = total + prices[items]
    return total

print compute_bill(shopping_list)

With the

for items in food

We are saying whatever is in FOOD we want the information that's stored inside there.

What I don't understand is how we can use ITEMS inside this

prices[items]

How is the word ITEMS now linked with the informations inside PRICES when the FOR is linked with FOOD.

Can you explain this?


11. making a purchase
#2

you just did.

items is your iterator in the for loop:

for items in food

so in order, items get assigned each item in food list.

Given the items in food are the same as the key values of the prices dictionary, you can use items to act as key for the dictionary. Look at this example (assuming prices dictionary is present):

x = "apple"
print prices["apple"]
print prices[x]

it doesn't matter if the key is stored in a variable (like your iterator) or you use a string directly, both is perfectly valid


#3

Thank you for the reply. It kind of makes a little more sense.

However, "You just did" is rather uncalled for. Seeing as you are a Codecademy Moderator, you may want to act a bit more professionally. Currently your attitude and mannerisms are incredible poor.


#4

good, you can play a bit more around with your code if you want, do you have more questions about it?


#5

Currently no.

I am making my way through the lessons.

It can get very confusing sometimes. I seem to figure out what needs to do without really understanding what I've done, which is more frustrating that not knowing the answer at all. If that makes any sense.


#6

You can add always add print statements to see what your code is doing.

That makes sense, it requires practice to overcome this problem.

You can always use this tool, select python 2.7 as language, this will tell you which step of the programming is executing, combined with some print statement, this should help


#7

Cool, I'll give that ago.

I'm from a 3D Graphical background and a software used is Maya which is build using Python, so I thought I'd start there.

Would love to build an App one day. You gotta start somewhere I guess.


#8

Maya can not be build using only python. For such a program, python wouldn't be suitable. I can't find what MAYA is build in.

there is a python and C++ API, that is a difference :wink:


#9

Written in C++,[1] MEL, Python,[2] C#

Not just Python then, but the code used to create stuff within maya is Python :slight_smile:


#10

Okay, cool, you know that :slight_smile: That you have encounter a unexpected surprise :slight_smile:

Yea, from what i read both Python and C++ can be used to create stuff in python :slight_smile:


#11

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.