简体   繁体   English

带2个循环的while循环内部循环到错误的位置Python3

[英]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. 我有一个用python 3编写的真正的基本计算程序。第一个是这样,因此在给出答案后可以提示用户执行另一个问题。 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. 您的问题是problem不会超出范围,因此仍然被定义,您跳出了第一个循环。

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. 在此之前未定义problem But it is defined after this. 但这在此之后定义的。 So when the outer while loop loops, problem will remain defined. 因此,当外部while循环循环时,问题仍然存在。 Thus, your second if clause (the else ) will execute, breaking. 因此,您的第二个if子句( else )将执行并中断。

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. 正如纳撒尼尔(Nathaniel)所说,我对problem定义没有确定。 However if I just set problem = None prior to the loop then there are no instructions to prompt the user to choose a problem. 但是,如果我在循环之前仅将problem = None设置problem = None ,则没有说明提示用户选择问题。

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. 感谢Nathaniel朝正确方向的观点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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