Print returning none

Hi everyone,

I started self learning python and I have no background of programming before.
Trying to self study from youtube.

I’m practicing if else, while and so on but for some reason the print is returning none.
I tried to google but still do not understand what’s going.

The simple code I’m trying to do is just converting F to C and Inch to cm

Here is the code:

print('Welcome to convertion, please select one: ')
print('1 = Fahrenheit to Celcius')
print('2 = Celcius to Fahrenheit')
print('3 = Inch to Cm')
print('4 = Cm to Inch')

conv = int(input(' '))

while conv >= 5:
    print('Please try again')
    conv = int(input(' '))

if conv == 1:
    ftoc = int(input(print('Fahrenheit: ')))
    ans = (ftoc - 32) * 5 // 9
    print(f'The asnwer is {ans} Celcius')

elif conv == 2:
    ftoc = (int(input(print('Celcius: '))))
    ans = (ftoc * 9 // 5) + 32
    print(f'The asnwer is {ans} Fahrenheit')

elif conv == 3:
    ftoc = (int(input(print('Inch: '))))
    ans = ftoc * 2.54
    print(f'The asnwer is {ans:.2f}Cm')

elif conv == 4:
    ftoc = (int(input(print('Cm'))))
    ans = ftoc / 2.54
    print(f'The asnwer is {ans:.2f}Inch')

Your input() functions do not also need the print() function.

e.g: ftoc = int(input('Fahrenheit: '))

I’ll not pull apart the rest of your code, because I can see you are very new at this and I’d hate to discourage you at this stage – it’s not a bad start.


To add: personally, I don’t think that watching youtube is the best way to learn the basics (but, each to their own). I’d recommend that you do more reading than watching.

Try this site:

Also, moving forward, please post your code between backtics with a python header…

```python
# this is where your code goes
```
2 Likes

Thank you for the input! really helpful. I just realized I do not need print when doing input as its redundant but to just understand why is it saying none? Just for the sake of knowledge.

I shall start reading, might just buy a book too.

2 Likes

Okay. The print() function has a return value of None which is why you see that on the console.

You’ll better understand that, when you lean more.

3 Likes

Hello, @crxwnz, and welcome to Python Software Foundation Discourse!

As you learn Python, and even after you gave achieved some expertise, it is a good idea to check the official documentation regularly.

See Python 3.11.0 documentation.

Regarding the current topic of discussion, see, in particular:

Note that for most of the functions, a return value is specified, but not for print(). In fact, the return value for that function is None.

2 Likes

Thank you for the input! really helpful. I just realized I do not need
print when doing input as its redundant

But useful to confirm that variable have the values you expected them to
have. Which we all do a lot when trying to figure out why our programmes
are not doing what we thought they should.

but to just understand why is it saying none? Just for the sake of
knowledge.

As mentioned already, print() returns None (like many functions
which “do something” rather than “compute something”).

What did you expect to see? Are you typing this at an interactive prompt
which normally prints the value of the last expression?

Cheers,
Cameron Simpson cs@cskk.id.au

1 Like

Celsius, not celcius.

When converting from Fahrenheit to Celsius, you should use ordinary division, not floor division:

‘’'python
ans = (ftoc - 32) * 5 // 9 # Wrong
ans = (ftoc - 32) * 5 / 9 # Correct


That means some of your answers will include decimals, but that is the right answer. For example, 102°F is closer to 39°C but if you use floor division `// 9` you get 38°C instead.

If you want to round to the nearest degree, try this:

'''python
ans = round((ftoc - 32) * 5 / 9)
1 Like

oops, :slight_smile:
Thank you!

Learning a lot from you all. I did not expect so many responses.

I shall take note of the division, floor division and rounding off.

From my understanding before, doing // will automatically round it off but I guess I cannot do assumption. Thank you!

1 Like

from my initial understanding I needed to have print() in order to display something, without knowing that input() does the same with added user input. So I ended up having input(print()) which caused print() to return none.

Thank you!

“But useful to confirm that variable have the values you expected them to
have. Which we all do a lot when trying to figure out why our programmes
are not doing what we thought they should.”

I shall keep this in mind always :slight_smile:

print() always returns None but normally you are just throwing this value away by not using it for anything.


The right way of combining print() and input() would be:

print('Question:')
result = input()

To make it work the same way as input() with the prompt text, you have to suppress the newline in print():

print('Question: ', end='')
result = input()

from my initial understanding I needed to have print() in order to
display something,

Loosely speak, you do, unless something else is doing the printing for
you.

without knowing that input() does the same with added user input. So I
ended up having input(print()) which caused print() to return none.

Wrapping a call to print() as an argument inside a call to input()
has no effect whatsoever on the behaviour of print().

A call to print() will print a single newline character to the
standard output (generally your terminal unless you’ve made special
arrangements).

Your call to input() behaves the same with or without the print() as
a parameter. What you might by misinterpreting is that the newline
from print() affects your display. But the behaviour would be the same
if you’d written:

 print()
 input()

The newline character gets displayed as a carriage return and a linefeed
on a terminal, which moves your cursor to the left and then down a line,
effectively moving it to the “next line”. And then your input() call
runs.

Let’s see what input() says about itself:

 Python 3.9.13 (main, Aug 11 2022, 14:01:42)
 [Clang 12.0.0 (clang-1200.0.32.29)] on darwin
 Type "help", "copyright", "credits" or "license" for more information.
 >>> help(input)
 Help on built-in function input in module builtins:

 input(prompt=None, /)
     Read a string from standard input.  The trailing newline is stripped.

     The prompt string, if given, is printed to standard output without a
     trailing newline before reading input.

     If the user hits EOF (*nix: Ctrl-D, Windows: Ctrl-Z+Return), raise EOFError.
     On *nix systems, readline is used if available.

 >>> 

When you called input(print()) then following happens:

  • print() is called to compute the argument to pass to input()
  • print() does its output and then returns a value, and that value is
    None, as mentioned
  • so you then call input(None), being the value from print
  • according to the help above, input only issues a prompt string “if
    given”
  • as defined, if a prompt isn’t given, the default value is None,
    and that’s what print() returned, so input() still behaves as if
    you had not given it a prompt string

This isn’t as large a coincidence as you might think, because in Python
the None value is what we almost always pass around as a placeholder
for “no value supplied/specified”.

“But useful to confirm that variable have the values you expected them
to have. Which we all do a lot when trying to figure out why our programmes
are not doing what we thought they should.”

I shall keep this in mind always :slight_smile:

Aye.

My computer always does exactly what I tell it to do but sometimes I
have trouble finding out what it was that I told it to do.

Cheers,
Cameron Simpson cs@cskk.id.au