当条件满足时,while循环不会终止

时间:2019-12-13 16:23:01

标签: python python-3.x

在Python中进行编程时,我陷入了while循环即使在满足条件之后也没有终止的情况下陷入困境的情况

代码如下:

print('--- Alex\'s Calculator ---')
print('1. ADDition')
print('2. SUBstraction')
print('3. MULtiply')
print('4. DIVide')
print('5. EXIT')
x = int(input())

command = ' Enter Your Two numbers To Perform The Operation : '
def ini():
    a = int(input())
    b = int(input())
    return a, b
def resultoo():
    result = ' Your Result after Performing The Operation from {} and {} is {}'
    print(result.format(a,b,c))
    print(' Want To Continue If Yes then Enter Your Choice else Press any number exept 1 - 4')
    x = int(input())

while x < 5:
    if x == 1:
        print(command)
        a, b = ini()
        c = a + b
        resultoo()
    elif x < 5:
        break

3 个答案:

答案 0 :(得分:1)

正如注释中指定的kuro一样,您的while循环看不到x,因为x在resultoo()中是本地的。

要轻松解决,只需添加:

return x

在resultoo()结尾

x = resultoo()

在while循环中

答案 1 :(得分:0)

您可以对此使用global var,更改此:

def resultoo():
    result = ' Your Result after Performing The Operation from {} and {} is {}'
    print(result.format(a,b,c))
    print(' Want To Continue If Yes then Enter Your Choice else Press any number exept 1 - 4')
    x = int(input())

进入:

def resultoo():
    global x
    result = ' Your Result after Performing The Operation from {} and {} is {}'
    print(result.format(a,b,c))
    print(' Want To Continue If Yes then Enter Your Choice else Press any number exept 1 - 4')
    x = int(input())

说明:

x是一个全局参数,在函数闭包之外是相同的,但不在函数内部,函数具有它自己的参数,因此,如果要更改在外部初始化的全局参数该函数,则需要先调用global语句,这将使x成为全局x

答案 2 :(得分:-1)

输入选项5后,您要退出。


我加了

import sys

并更改

elif x < 5:

elif x == 5:

并添加

sys.exit(0)

我还添加了getMenu()函数

这是在我的编辑器中运行的完整代码:

import sys


def ini():
    command = ' Enter Your Two numbers To Perform The Operation : '
    print(command)
    a = int(input())
    b = int(input())
    return a, b


def resultoo(a, b, c):
    result = ' Your Result after Performing The Operation from {} and {} is {}'
    print(result.format(a, b, c))


def getMenu(x):
    if x == 0:
        print("Choose menu item")
        x = int(input())
    elif x != 0:
        print(' Want To Continue If Yes then Enter Your Choice else Press any number exept 1 - 4')
        x = int(input())

    return x


def main():
    x = 0
    while x < 5:
        print('\n\n1. ADDition')
        print('2. SUBstraction')
        print('3. MULtiply')
        print('4. DIVide')
        print('5. EXIT\n')

        x = getMenu(x)

        if x == 1:
            a, b = ini()
            c = a + b
            resultoo(a, b, c)

        elif x == 5:
            sys.exit(0)

        else:
            print("No valid menu item")


if __name__ == '__main__':
    print('----------------------------------------------------------------------------------------------------------')
    print('-------------------------------------------- Alex\'s Calculator -------------------------------------------')
    main()

我还格式化了您的代码(Pycharm中的alt + Enter)以符合PEP8标准;)