0

So I'm trying to learn python, don't have any background on programming picked up a simple calculator project, in it I don't want the program to crash everytime the user inputs something other than a number so I pass the input through a try/except. Thing is, if the user inputs a letter on the first "loop", on the second I get a "NoneType" kind of data, here's my code for the try/except:

def user_num():
    num = input(">>>")
   
    try:
        int(num)
        return num
    except:
        print("I need a number.")
        user_num()
print("First number:")
x1 = user_num()
  • In general, few Python functions do 'in place' conversions. So `int(num)` does not convert the named value still pointing to `num` into an int. You need to do something like `num=int(num)` or better `return int(num)`. Just calling `int(num)` does the conversion but then the retuned value is lost since it is not named anything or used afterwards. – dawg Sep 24 '22 at 14:28

0 Answers0