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 -- https://mail.python.org/mailman/listinfo/python-list
Re: Output showing "None" in Terminal
Thank you for the clarification. What I'm trying to achieve here are: User be able to choose miles or kilometers to convert. When selected (mi/km), prints out the user input and the answer. km to mi = km/1.609 mi to km = mi*1.609 Thank you again! On Mon, Aug 24, 2020 at 1:41 PM Calvin Spealman wrote: > How are you actually running your code? > > "None" is the default return value of all functions in Python. But, the > interpreter is supposed to suppress it as a displayed result. > > As a side note, both your km_mi() function and the line "answer = km_mi" > are certainly wrong, but it is not clear what you intend to do. > > On Mon, Aug 24, 2020 at 4:25 PM Py Noob wrote: > >> 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 >> -- >> https://mail.python.org/mailman/listinfo/python-list >> >> > > -- > > CALVIN SPEALMAN > > SENIOR QUALITY ENGINEER > > cspea...@redhat.com M: +1.336.210.5107 > [image: https://red.ht/sig] <https://red.ht/sig> > TRIED. TESTED. TRUSTED. <https://redhat.com/trusted> > -- https://mail.python.org/mailman/listinfo/python-list
Re: Output showing "None" in Terminal
Thank you so much for the help. I'm self-studying and watching tutorials on youTube. The problem was given as an exercise after the tutorial. I did modify my code based on the suggestions here and it helps. Thank you! On Tue, Aug 25, 2020 at 4:31 PM Schachner, Joseph < joseph.schach...@teledyne.com> wrote: > 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 > Sent: Monday, August 24, 2020 9:12 AM > To: python-list@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 > > -- https://mail.python.org/mailman/listinfo/python-list