pythonprintingreturnprogram-entry-point

Python main() does not recognize returned value


1- I get this error when I try to print a return value from inside main => " name 'conversion' is not defined " 2- I have some dummy prints to debug my code

def main():
    time = input("What time is it? ")
    convert(time)
    **print (conversion)**

def convert(time):
    hours, minutes = time.split(":")
    print("==1== " + f'{hours = }' + f'{minutes = }')
    print("==2== " + f'{hours = }')
    print("==3== Float of hours", float(hours))
    print("==4== Float of minutes", float(minutes))
    conversion = float(hours)+(float(minutes)/60)
    print ("==5== ",f'{conversion = }')
    print ("==6== ", conversion)
    return conversion

if __name__ == "__main__":
    main()

I tried commenting out the print (conversion) inside main. If I do, I get no errors but I am supposed to return a decimal value back to main


Solution

  • Merely calling a function does not automatically retrieve its returned value, as you seem to expect.

    You need to assign its returned value to a variable:

    def main():
        time = input("What time is it? ")
        conversion = convert(time)
        print (conversion)
    

    And you knew this already, because you did properly assign time to the returned value from input() on the previous line.