Error in code I cant find for "sals shipping" project

Why doesn’t anything get printed to the console when I use the code below?

It’s for the “Learn Python 3” “Sals Shipping” project

premium_ground_ship = 125.00

def normal_ground_ship(weight):
  if weight >= 10:
    return (weight * 4.00) + 20
  elif weight >= 6:
    return (weight * 4.00) + 20
  elif weight >= 2:
    return (weight * 4.00) + 20
  else:
    return (weight * 4.00) + 20

def drone_ship(weight):
  if weight >= 10:
    return weight * 14.25
  elif weight >= 6:
    return weight * 12.00
  elif weight >= 2:
    return weight * 9.00
  else:
    return weight * 4.50

def cheapest_price(weight):

 ground = normal_ground_ship(weight)
 premium = premium_ground_ship
 drone = drone_ship(weight)

 if ground < premium and ground < drone:
   cost = ground
   method = "normal ground shipping"
 elif premium < ground and premium < drone:
   cost = premium_ground_ship
   method = "premium ground shipping"
 else:
  cost = drone_ship
  method = "drone shipping"
   
  print(
       "The cheapest shipping method is " + method + " and it will cost you $ " + cost + "."
    )

cheapest_price(4.8)
cheapest_price(41.5)

P.S I dont know if this tag is right

Hey there @css7985072411 and welcome to the Codecademy forums!! :slightly_smiling_face:

There are a couple things to look at here. First check your indentation on the print() in cheapestPrice(). Second take a look at what you are trying to print() out:

print(
       "The cheapest shipping method is " + method + " and it will cost you $ " + cost + "."
    )

If cost is an integer how are you going to combine that with a string?

1 Like

The print statement is nested within the else clause/body.

so only when else is executed, print() is reached. Is that desired?

@8-bit-gaming is right that there is an indention problem, but you should also understand the flow and the implications this has

2 Likes

Thank you both for your help, when I fixed the integer and the else and print being connected I ended up with “The cheapest shipping method is normal ground shipping and it will cost you 39.2. None The cheapest shipping method is premium ground shipping and it will cost you 125.0.
None” when I printed it to the console, why is there 2 “None” after both sentences?

The code now looks like this at the last part

def cheapest_price(weight):

 ground = normal_ground_ship(weight)
 premium = premium_ground_ship
 drone = drone_ship(weight)

 if ground < premium and ground < drone:
   cost = ground
   method = "normal ground shipping"
 elif premium < ground and premium < drone:
   cost = premium_ground_ship
   method = "premium ground shipping"
 else:
  cost = drone_ship
  method = "drone shipping"
   
 print(
       "The cheapest shipping method is " + method + " and it will cost you $ " + str(cost) + "."
    )

print(cheapest_price(4.8))
print(cheapest_price(41.5))
1 Like

you call print on the cheapest_price function call:

print(cheapest_price(4.8))

which will print the returned value of cheapest_price, but this function does not return anything, so you get None (the absence of a return value)

I would also recommend to follow pep style guide:

PEP 8 -- Style Guide for Python Code | Python.org

which recommends to use 4 spaces, I personally find this more readable then 2 spaces.

1 Like

Okay thanks, the code is now working and ill taked a look at that guide. For future refrences is the “Classes” tag the place to ask these kind of questions?

I think being in the Python category is probably already good enough. For this post, I would have added general. Classes is in reference to another element of object oriented programming languages used to store different data type.

You will learn about these later on in the Python coursework, however some examples of classes you have already seen are the built in string, int, and float.

1 Like

Good afternoon sir. Happy new year!!! please my code works and is correct in the context of this exercise. the problem is that i was trying to add user input as in Input(“please input weight”) such that the user can input the weight of their cargo into the system but i keep getting different errors every way i try especially an EOFError. please can you advise me on how to do this? thank you sir.

def ground_shipping(weight):
if (weight <= 2):
cost = (20 + (1.5 * weight))
elif (2 < weight <= 6):
cost = (20 + (3 * weight))
elif (6 < weight <= 10):
cost = (20 + (4 * weight))
else:
cost = (20 + (4.75 * weight))
return cost

premium_ground_shipping = 125

def drone_shipping(weight):
if (weight <= 2):
cost = (4.5 * weight)
elif (2 < weight <= 6):
cost = (9 * weight)
elif (6 < weight <= 10):
cost = (12 * weight)
else:
cost = (14.25 * weight)
return cost

def cheapest_package(weight):
if ground_shipping(weight) < (drone_shipping(weight) and premium_ground_shipping):
return "You should ship using ground shipping, it would cost " + str(ground_shipping(weight))
elif drone_shipping(weight) < (ground_shipping(weight) and premium_ground_shipping):
return "You should ship using drone shipping, it would cost " + str(drone_shipping(weight))
else:
return "You should ship using premium ground shipping, it would cost " + str(premium_ground_shipping)

print (cheapest_package(4.8))

An EOFError being thrown while using input() in the Codecademy IDE is most likely due to that particular lesson not having a console capable of receiving user input. There are not many lessons I remember that are able to use the input() function. You could of course copy your work over to your device or an online IDE and add user input to it there.

1 Like

ok thank you very much. but please how would you suggest i introduce input() into the code. thanks in advance. happy new year!!!

8-bit-gaming already covers this in his answer?

The lesson does not have this capability, so you can either decide:

  1. to not use input()
  2. run your code somewhere else, either on your computer or some other website like repl.it

but then again, this has already been covered by 8-bit-gaming.

2 Likes