Hello , i wanted to ask that in the topic making purcahse . i have writted a code and getting error , but in my point of veiw it is right . please help with this problem . i would be thankfull to you


#1

my code is
shopping_list = ["banana", "orange", "apple"]

stock = {
"banana": 6,
"apple": 0,
"orange": 32,
"pear": 15
}

prices = {
"banana": 4,
"apple": 2,
"orange": 1.5,
"pear": 3
}

Write your code below!

def compute_bill(food):
total = 0
if i in food:
total = total + prices[i]

return total

NameError: global name 'i' is not defined . how to overcome it
#2

Hello @r242,

please enclose your code in triple backticks (`). Because it isn't formatted properly.


#3

I am giving you the full code , which I have written for this
def compute_bill (food)
total = 0
if i in food:
total = total + prices[i]

                       return total

#4

can you please explain me , is this my code is right or wrong and please also give the wright answer of this statement


#5

Hey @r242,

Try using a for loop instead to iterate over all the items in the food list you are passing to compute_bill


#6

can you please give me the answer , i am not getting it and also please explain me , I wanted to learn the python in my vacations , I am 13 years old . and wanted to do something with python language


#7

Hey there

It's really hard to figure out what you're doing if you aren't allowing those looking to help to reproduce what you are doing.

See how the indentation is all messed up in your post? If somebody copies it and tries to run it, they will not get the same result as you describe, and will therefore not be able to look at the same code.

So, post it in a way that when copied, it does exactly what it does for you. Also describe what it is doing differently from what you want it to do. If there is an error message that you don't understand or know what to do with, then you'll have to include that error message!

Generally, just make it as easy as possible to help you

Providing the right information also shows that you've tried. If you don't then you'll only get responses about how to gather and present more information, because that's what you seem to be missing!


#8

Hey @r242,

By Codecademy's community guidelines, I am not allowed to give you the code, Just give you hints and tips for you to understand and arrive at the solution yourself. But what I can do is explain you the logic behind the code.

What censor is supposed to do is, analyse the user's input word by word, and if a word matches the censor, it replaces it with asterisks. You must .split() the user's input, iterate over it using a for loop, use an if statement to determine whether a word is == censor, and if it is, add the amount of asterisk as the word length to a variable, else add the word itself to the variable, and return that variable...

Hope this clears things out for you...


#9

"Oops, try again. compute_bill(['apple']) resulted in a NameError: global name 'i' is not defined " I am getting this error . why it is coming


#10

Can you please post your updated code? Thanks!


#11

That's the error message, but you still haven't provided a way of reproducing it! There's nothing for us to relate it to.

It does say quite a lot though, so before you continue asking, look at what you're doing with the variable name that it mentions.

Error messages are there for you to consider, you're going to make mistakes all the time so reading error messages is a required skill!


#13

your code is wrong .
you have written
if i in food
it should be
for i in food


#14

Hey there @r242,

Like @alokrp said there is a small mistake in your code, a case of changing if into for. Let me try and explain that to you.

This is the code you've shared with us, I'm just assuming with this indentation aswell, correct me if I'm wrong.

def compute_bill (food)
    total = 0
    if i in food:                                        <- here
        total = total + prices[i]
    return total

In your code you've almost made a for-loop, but with an if keyword.
The correct syntax for a for-loop is:

keyword
  | var  target list
  | |     |
  v v     v
for i in list
    //the code that should be executed

You start with the for keyword, to let python know you're making a for-loop.
Then you define the variable in which you are going to store each element you are looping over.
Then you tell python over which list we're looping.

Now, python will execute the code in the for-loop for each element in the given list. An example:

prices = {
    "chocolate": 5,
    "cookie": 2,
    "cake": 3
    }

for item in prices
    print item + prices[item]

The example above will loop over each item in the list/collection "prices" and print out the item name + the item price. The output will look something like this:

chocolate 5
cookie 2
cake 3

In the case of this question we say:
for each food in the list of foods.

The example above isn't code, but a way of explaining what we're doing in the code.
I hope you understand the basic principle of for-loops and if-statements.


#15

thank you so much , I have understand it ,
thank you very much


#16

oh!!!! , I have just seen it thanks for it