Wanted to make it shorter and "while" is not working now

Question:

I had this code:

num1, check = None, None
proceed_answer = input('Great, we have one more game, wanna play? (y/n): ')

if proceed_answer == 'y':
    while True:
        if num1 is None or not check_valid_number(num1):
            try:
                num1 = float(input('Enter one valid number: '))
            except ValueError:
                print('It must be a number!')
                continue
            if not check_valid_number(num1):
                print('It must be greater than 0!')
                continue
        if check is None or not check_valid_number(check):
            try:
                check = float(input('Enter one more valid number: '))
            except ValueError:
                print('It must be a number!')
                continue
            if not check_valid_number(check):
                print('It must be greater than 0!')
                continue
        if even_division(num1, check):
            print(f'{check} divides evenly into {num1}')
        else:
            print(f'{check} does not divide evenly into {num1}')
        break
elif proceed_answer == 'n':
    print('Okay, bye')

But I wanted to make it more sophisticated since I think it is a bit messy, and I came up with this, which seems to not pup up any errors on VS code:

num1, check = None, None
proceed_answer = input('Great, we have one more game, wanna play? (y/n): ')

if proceed_answer.lower() == 'y':
    while num1 is None or not check_valid_number(num1):
        try:
            num1 = float(input('Enter one valid number: '))
        except ValueError:
            print('It must be a number!')
            continue
        if not check_valid_number(num1):
            print('It must be greater than 0!')
            continue
        try:
            check = float(input('Enter one more valid number: '))
        except ValueError:
            print('It must be a number!')
            continue
        if not check_valid_number(check):
            print('It must be greater than 0!')
            continue
        if even_division(num1, check):
            print(f'{check} divides evenly into {num1}')
        else:
            print(f'{check} does not divide evenly into {num1}')
else:
    print('Okay, bye')

It runs perfectly fine until the second try, where if I enter a number smaller than 0 or a letter, it shows the corresponding message, but the program stops. I added the continue so i don’t understand why it stops. The first code works fine and the second one brings the error I described.

functions:

def check_valid_number(data):
    return 0 <= data

def even_division(inp1, inp2):
    return inp1 % inp2 == 0

I know check_valid_number function is unnecessary but i want to practice functions as much as possible right now.

Thank you all.

I am expecting both "versions" to work the same way. I tried asking chat gpt but he starts making more difficult code.

Asked By: Foo127

||

Answers:

You are missing while statement for check variable. In case of invalid value or None value for check, continue will point to return to this while statement.

num1, check = None, None
proceed_answer = input('Great, we have one more game, wanna play? (y/n): ')
if proceed_answer.lower() == 'y':
    while num1 is None or not check_valid_number(num1):
        try:
            num1 = float(input('Enter one valid number: '))
        except ValueError:
            print('It must be a number!')
            continue
        if not check_valid_number(num1):
            print('It must be greater than 0!')
            continue
    while check is None or not check_valid_number(check):   
        try:
            check = float(input('Enter one more valid number: '))
        except ValueError:
            print('It must be a number!')
            continue
        if not check_valid_number(check):
            print('It must be greater than 0!')
            continue
    if even_division(num1, check):
        print(f'{check} divides evenly into {num1}')
    else:
        print(f'{check} does not divide evenly into {num1}')
else:
    print('Okay, bye')
Answered By: nithish08
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.