Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading