Output showing "None" in Terminal

Schachner, Joseph Joseph.Schachner at Teledyne.com
Tue Aug 25 19:31:24 EDT 2020


The very first line of your function km_mi(): ends it:
def km_mi():
    return answer

answer has not been assigned, so it returns None.

Advice: remove that "return" line from there.  Also get rid of the last line, answer = km_mi which makes answer refer to the function km_mi().
Put the "return answer" line at the end, where the "answer=km_mi" used to be.

That should help.  The code calculates "answer".   It prints "answer".   You should return "answer" at the end, after it has been calculated.

--- Joseph S.

-----Original Message-----
From: Py Noob <pynoob76 at gmail.com> 
Sent: Monday, August 24, 2020 9:12 AM
To: python-list at python.org
Subject: Output showing "None" in Terminal

Hi!

i'm new to python and would like some help with something i was working on from a tutorial. I'm using VScode with 3.7.0 version on Windows 7. Below is my code and the terminal is showing the word "None" everytime I execute my code.

Many thanks!

print("Conversion")

def km_mi():
    return answer

selection = input("Type mi for miles or km for kilometers: ")

if selection == "mi":
    n = int(input(print("Please enter distance in miles: ")))
    answer = (1.6*n)
    print("%.2f" % answer, "miles")

else:
    n = float(input(print("Please enter distance in kilometers: ")))
    answer = (n/1.6)
    print("%.2f" % answer, "kilometers")

answer = km_mi



More information about the Python-list mailing list