Input statement is resulting with None

Question:

I’m trying to call a function within a function that asks for some input. It runs and gives me the right answer, but not before showing "None".

def func1(f_temp):
    c_temp = round(float((f_temp - 32) * 5 / 9),1)
    print(f_temp, "degrees Fahrenheit is equal to", c_temp, "degrees Celsius.")

def func2(mph):
    mps = mph * 0.44704
    print(mph, "miles per hour is equal to", mps, "meters per second.")

def main():
    print("Enter 1 to convert Fahrenheit temperature to Celsius.")
    print("Enter 2 to convert speed from miles per hour to meters per second.")
    main_input = int(input())
    if main_input == 1:
        f_temp = input(print("Please enter a temperature in Fahrenheit: "))
        return func1(f_temp)
    elif main_input == 2:
        mph = input(print("Please enter a speed in miles per hour: "))
        return func2(mph)
    else:
        print("Please enter 1 or 2 only.")

And here is what happens when I run it:

main()
Enter 1 to convert Fahrenheit temperature to Celsius.
Enter 2 to convert speed from miles per hour to meters per second.
1
Please enter a temperature in Fahrenheit: 
None52
52.0 degrees Fahrenheit is equal to 11.1 degrees Celsius.
Asked By: sidewaysblue

||

Answers:

Change input(print(…)) to just input(…). Print returns None and input() is printing it. (as what @Mark Tolonen said)

def func1(f_temp):
    c_temp = round(float((f_temp - 32) * 5 / 9),1)
    print(f_temp, "degrees Fahrenheit is equal to", c_temp, "degrees Celsius.")

def func2(mph):
    mps = mph * 0.44704
    print(mph, "miles per hour is equal to", mps, "meters per second.")

def main():
    print("Enter 1 to convert Fahrenheit temperature to Celsius.")
    print("Enter 2 to convert speed from miles per hour to meters per second.")
    main_input = int(input())
    if main_input == 1:
        print("Please enter a temperature in Fahrenheit: ", end="")
        f_temp = input()
        return func1(f_temp)
    elif main_input == 2:
        print("Please enter a speed in miles per hour: ", end="")
        mph = input()
        return func2(mph)
    else:
        print("Please enter 1 or 2 only.")
Answered By: Vlone

The input() function receive a string and print it, the print() function print a string in the terminal but returns nothing (None),

f_temp = input(print("Please enter a temperature in Fahrenheit: "))

you are calling print inside input, so input waits for a string and only gets a None and print it, this is why your program writes None in some point. You can fix it :

f_temp = input("Please enter a temperature in Fahrenheit: ")
Answered By: Mauxricio
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.