I've been learning Python for about 5 days and I have no prior programming experience, so apologies if this is too basic a doubt; I've checked some other similar questions but the issue wasn't the same.
I'm defining some input check functions for the program that I'm making and my idea was to rely on while loops and boolean conditions to make them work, but I found out that the loop doesn't end regardless of the input being valid.
This is the simplest one I've written:
def is_num(num):
try:
num = float(num)
num_check = True
except:
print("Incorrect value. Please, enter a number.")
num_check = False
while not num_check:
user_num = input("Enter a number: ")
is_num(user_num)
print("Valid number.")
I do get the error message if I enter something other than a number, but if I do enter a number I just get the input prompt over and over.
I thought maybe the problem was coming from num_check
only being defined inside the function so I tried out this version, but it changed literally nothing:
def is_num(num, num_check):
try:
num = float(num)
num_check = True
except:
print("Incorrect value. Please, enter a number.")
user_num_check = False
while not user_num_check:
user_num = input("Enter a number: ")
is_num(user_num, user_num_check)
print("Valid number.")
What should I change for this to work? Thanks!