Sal's Shipping project problem

Hi ,I have stuck with this code :S Can you plase help me , and tell me what is wrong with my cod
:frowning:


I get Traceback (most recent call last):
File “script.py”, line 40, in
%(method,cost))
NameError: name ‘method’ is not defined ,
How “method” is not defined ?
I defined it … “method=standard ground” … “method=drone transport” …etc.

It’s the scope. method only exists within the function you declared it. Referring to it outside will throw an error.

Welcome to the forums :slight_smile:

If you move the print statement to the end of the function (note, indentation matters) it would work.


Again , when I paste “revised” code , there is again error. :frowning
Traceback (most recent call last):
File “script.py”, line 40, in
%(method,cost))
NameError: name ‘method’ is not defined

look at your indentation

  else:
       method="premium ground transport"
       cost=premium
print("Best price for you is $%.2f with %s shiphing"   
       %(method,cost))    
print_cheaphest_shiphing_method(4.8)

Should probably be closer to

  else:
       method="premium ground transport"
       cost=premium
       print("Best price for you is $%.2f with %s shiphing" %(method,cost))
       #i'm unfamiliar with this style of printing, but as long as it's a valid print it should work
print_cheaphest_shiphing_method(4.8)

After I do that , I do not get error , but i dont get any result . I only get 53.6 and 6.75 ,ant nothing .
https://gist.github.com/45b625891ae265e2b80fd52ecd27ac64

My mistake (also with indentation).

I wrote this

  else:
       method="premium ground transport"
       cost=premium
       print("Best price for you is $%.2f with %s shiphing" %(method,cost))
       #i'm unfamiliar with this style of printing, but as long as it's a valid print it should work
print_cheaphest_shiphing_method(4.8)

Should be this:
Indentation matter!!

  else:
       method="premium ground transport"
       cost=premium
  print("Best price for you is $%.2f with %s shiphing" %(method,cost))
       #i'm unfamiliar with this style of printing, but as long as it's a valid print it should work
print_cheaphest_shiphing_method(4.8)

Now I get :" Traceback (most recent call last):
File “script.py”, line 41, in
print_cheaphest_shiphing_method(4.8)
File “script.py”, line 40, in print_cheaphest_shiphing_method
%(method,cost))
TypeError: must be real number, not str

Look at your string formatting

print( "Best price for you is $%.2f with %s shiphing" %(method,cost) )

What would you think the program would try to interpret with %?
In this case, the order of the %() parameters counts. They have to reflect the order they appear in the string.

https://www.python-course.eu/python3_formatted_output.php

Each error message is a clue and opportunity to get to know the language better. You should definitely look for documentation/references because not only will they help you solve the problem, but they will give you the foundational reason as to why it is (which is much more important).

Especially while learning, “getting the code to run” is not as important as building foundational knowledge. I’d rather have my code break every 10 minutes and learn something than have something that runs fine but I don’t learn anything from it.

1 Like

I absolutely understand, but I have a problem here that I can’t continue to learn, I’m going back to the beginning of the course 3 times, doing everything from the beginning and I can’t run the program again. Please, just write me a code so they can run. Is there a mistake on the website and instuction? I follow video instruction and do same like him ,but there is only errors .

Screen Shot 2020-08-05 at 3.31.26 PM

Just flip cost and method. It’s thinking method is a real number, and cost is a string. Hence the error.

Oh ,my god there is nothing again

I found a solution.Print must be on this postition .
Screenshot (10) Thank you so much, and I’m sorry if I upset you.

No problem, not upset at all! I’m happy to help.