Sal's Shipping

Hi yall

I got it all perfectly but the only thing is that when I print it I get these 2 “None” messages after each print statement. Is there anything I am missing here? And also when I was working on the print statement I had to look up a way to do what I did and I came up with the %s for the string and %.2f for the number with 2 decimal places but I am still not sure if I understand the why.
I leave a ss here thank you!!!

Hey there @dev0552860502 :grinning:

In the future could you please format your code instead of using a screenshot?
Simply press this button:

Than insert your code between the two rows of backticks:

Formatting allows your code to be copy/pasted, and preserves indentation.


The print() function prints the value of what it is passed as an argument, in this case your function, print_cheapest_method(), has no return statement, and as such there is no value for your print() to put on the console.

By this point I believe you have learned about the use of the return statement, but as a remainder, it is used to send a value back from a function to the place it was called. For example:

def add(n1, n2):
  return n1 + n2
#    \
#    return statement sends back the value of n1 and n2

number = add(3, 5)   # number = 8
print(add(3, 5))     # prints 8

Since your function has a print() statement, you could call it by itself:

def add(n1, n2):
  print(n1 + n2)

add(3, 5) 
#prints 8

Though your function is much more limited this way, as you can not store the value. Adding a return would make it much more flexible.

2 Likes

Thank you! What would be the correct way to this code then? Or the most precise

1 Like

Do you mean whether to use a return or print in a function? Generally, it is best to return a value, however if you wanted a function to be purely for the user’s benefit, then maybe you should use print isntead:

def ask_user_about_their_day():
   day = input("How was your day?")
   if day == "good":
     print("That's good")
   else:
     print("bad luck")

That was a very random example, but in this case, you may not want that function to return anything, just simply interact with the user.

2 Likes

I would agree with @codeneutrino here. I almost always try to use a return statement, because I can always print() out the value returned:

def bigger_than(n1, n2):
  if n1 > n2:
    return True
  else:
    return False


print(bigger_than(3, 4)) # False

However if I put the print() inside my function and do not include a return than the function becomes greatly limited, as I can not use that value for other purposes in the future.

Another option to consider is using both a return and a print().
For example, suppose one is creating a machine learning program and using the function @codeneutrino proposed. If one returns the computers answer, or the variable day, than it could be used later on in the program to effect how the program interacts with the user.

This would of course be a more advanced project than I am guessing you are at right now, but just an example. Another example could be a different version of my add() function:

def users_add():
  n1 = int(input('n1: '))
  n2 = int(input('n2: '))

  print(n1 + n2)
  return n1 + n2

add()
user_number = add()

You can see here that now you can either just print() out what you want by calling the function, or you can also save the value to a variable.

The only drawback to having a print() on top of a return (That I know of :wink:) is the fact it will always print to your console, which could get messy if you used it many times.
I have not done much studying on how memory gets taken up by a Python program, so it could be that this may slow a program down, once that program has gotten very large.

2 Likes