-1

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!

  • 2
    If you change a boolean inside a function, that does not modify the boolean outside the function. You should return the boolean instead – rdas Apr 29 '20 at 17:15
  • You never change user_num_check regardless of the input you provide, so the while loop keeps running regardless of the input you provide. – LTheriault Apr 29 '20 at 17:16
  • Read about global and local variables in Python functions. – Michael Butscher Apr 29 '20 at 17:17
  • @L Uhm... but user_num_check is False before the input and then it should always change to True unless the float() command fails, right? I'm probably missing something very obvious here... – Gaploser Apr 29 '20 at 18:22
  • @Michael Thank you! This was the answer I was looking for, I just had to add a `global num_check` line to the function and it works perfectly. – Gaploser Apr 30 '20 at 03:31

2 Answers2

0

Generally you need a condition to break the while loop . Just having while True will keep running the loop. when do you want to exit out of the loop?

may be a simple code like this will help

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: ")
    if not is_num(user_num, user_num_check):
        break
    print("Valid number.")
Swam
  • 19
  • 2
  • Hey, thanks for your answer! This works for valid input (once you indent the last line to the right), however it doesn't loop back to asking for input when it's invalid, it just displays the error message. However, I don't really understand how the condition `if not is_num(user_num, user_num_check)`. works. My logic tells me it should (maybe) trigger if the `is_num` function hasn't been called, but the fact that the loop does break probably means I've got it wrong. – Gaploser Apr 29 '20 at 18:36
0

When the answer is correct, you need to exit the loop.

To do that, you could use a else statement.

So, this would be your code:

def is_num(num):
    try:
        num = float(num)
        num_check = True
    except:
        print("Incorrect value. Please, enter a number.")
    else:
        print("Valid number.")
num_check = False
while not num_check:
    user_num = input("Enter a number: ")
    is_num(user_num)

Hopefully this helps with your question!

10 Rep
  • 2,217
  • 7
  • 19
  • 33
  • Thank you for taking the time to answer! However, I've run this code and it doesn't end the loop. It just prints"Valid number." before asking for an input again ^^U – Gaploser Apr 29 '20 at 18:28
  • In that case, put the entire thing in a loop. Look at the python docs on how to do that. Also, would you mind upvoting my question? Thanks! – 10 Rep Apr 29 '20 at 18:39