11. Need explanation


#1

Why print function doesn't print value?


#2

@prmishania: Anything you declare right after a return statement, won't be executed.
The execution of the program will read that instruction, ignore everything that's after that and will go back to where the function was called.

Q: But what happens if I have other lines of code after the return? I mean, outside of the function?
A: They might be executed, depends on how you wrote your code.

Example:

def functionTest():
     return 1
     print ("Test") #Won't be executed after the execution of the return
print ("Testing again") #Will be executed.

#3

I understand your point,but even when I commented #return or make print global it doesn't work.
And also strange to me, that it gives me a error



#4

@prmishania: That's because total was declared inside of your function compute_bill and therefore you won't be able to access it.

Q: Can I access a function's variable and print it out?
A: No, sorry.

If you use print, before calling the function, you'll get the value stored in total since that's what you used to calculate things.


#5

In other words, print the call expression:

print(compute_bill(grocery_list))