简体   繁体   中英

While loop w/ 2 loops inside looping to wrong spot Python3

I have a realy basic calculater program written in python 3. The first while is so the user can be prompted to do another problem after the answer is given. The subsequent loops are for validating the user inputs.

For some reason the loops inside work fine and the main one will work if I choose to close the program, however, if I choose to do another problem it loops to asking for a number instead of the beginning asking what problem.

while True:
# User inputs type of problem & input is validated
    while True:
        problem = input()

        if problem not in ('1', '2', '3', '4'):
            print('You did not make a valid selection, please try again.')
        else:
            break

# User inputs numbers for problems & input is validated
    while True:
        num1 = input('Please input the first number in the problem')
        num2 = input('Please input the second number in the problem')
        isnum1 = is_number(num1)
        isnum2 = is_number(num2)

        if isnum1 is False or isnum2 is False:
            print('You did not enter a number')
        else:
            num1 = float(num1)
            num2 = float(num2)
            break

# Perform appropriate math
    ans = 0
    if problem == '1':
        ans = num1 + num2
        print(num1, '+', num2, '=', ans)
    elif problem == '2':
        ans = num1 - num2
        print(num1, '-', num2, '=', ans)
    elif problem == '3':
        ans = num1 * num2
        print(num1, '*', num2, '=', ans)
    elif problem == '4':
        ans = num1 / num2
        print(num1, '/', num2, '=', ans)

# Ask if user would like to perform another problem or exit
    nxt = input('Would you like to do another problem?\n 1 - Yes\n 2 - No')

    if nxt not in ('1', '2'):
        print('You did not make a valid selection.')
    elif nxt == '2':
        break

exit(0)

Your issue is that problem does not leave scope, so is still defined and you break out of the first loop.

This is your first loop:

while True:
    problem = input()

    if problem not in ('1', '2', '3', '4'):
        print('You did not make a valid selection, please try again.')
    else:
        break

problem is not defined prior to this. But it is defined after this. So when the outer while loop loops, problem will remain defined. Thus, your second if clause (the else ) will execute, breaking.

To prove this, do this:

while True:
    problem = input()

    if problem not in ('1', '2', '3', '4'):
        print('You did not make a valid selection, please try again.')
    else:
        print('You have made a valid selection: {}".format(problem))  # This will display, showing that the loop is executed.
        break

To fix it, do this:

problem = None
while True:
    problem = input()

    if problem not in ('1', '2', '3', '4'):
        print('You did not make a valid selection, please try again.')
    else:
        print('You have made a valid selection: {}".format(problem))
        break

As Nathaniel stated, I left problem definied. However if I just set problem = None prior to the loop then there are no instructions to prompt the user to choose a problem.

So instead I changed this code:

    if nxt not in ('1', '2'):
        print('You did not make a valid selection.')
    elif nxt == '2':
        break

To this:

if nxt not in ('1', '2'):
    print('You did not make a valid selection.')
elif nxt =='1':
    print('Please select the type of problem you would like to complete.\n 1 - Addition\n 2 - Subtraction\n 3 - Multiplication\n 4 - Division\n')
    problem = 0
elif nxt == '2':
    break

Everything works great now. Thanks for the point in the right direction Nathaniel.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM