繁体   English   中英

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

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

我有一个用python 3编写的真正的基本计算程序。第一个是这样,因此在给出答案后可以提示用户执行另一个问题。 后续循环用于验证用户输入。

由于某种原因,如果我选择关闭程序,则内部循环会正常工作,而主循环会正常工作,但是,如果我选择执行其他问题,则循环会要求输入数字而不是开始询问什么问题。

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)

您的问题是problem不会超出范围,因此仍然被定义,您跳出了第一个循环。

这是您的第一个循环:

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 但这在此之后定义的。 因此,当外部while循环循环时,问题仍然存在。 因此,您的第二个if子句( else )将执行并中断。

为了证明这一点,请执行以下操作:

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

要解决此问题,请执行以下操作:

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

正如纳撒尼尔(Nathaniel)所说,我对problem定义没有确定。 但是,如果我在循环之前仅将problem = None设置problem = None ,则没有说明提示用户选择问题。

所以我改了这段代码:

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

对此:

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

现在一切都很好。 感谢Nathaniel朝正确方向的观点。

暂无
暂无

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

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