1

While programming in Python I got stuck in a case where the while loop is not terminating even after the condition is being satisified then also

the code is as follows:

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
wjandrea
  • 28,235
  • 9
  • 60
  • 81

3 Answers3

1

As kuro specified in the comment, x can't be seen by your while loop because it's local to resultoo().

To solve it easily just add :

return x

at the end of resultoo()

and

x = resultoo()

in your while loop

Maxime
  • 818
  • 6
  • 24
0

You can use global var to this, change the this:

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())

into:

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())

Explnation:

x is a global argument, that will be the same out of the function closure, but not inside of it, the function has it own params, so if you want to change a global argument that is initalizing outside the function, you will need to call the global statement before, that will make x the global x

wjandrea
  • 28,235
  • 9
  • 60
  • 81
Reznik
  • 2,663
  • 1
  • 11
  • 31
  • You shouldn't use `global` if you don't really need to. Read "[Why are global variables evil?](https://stackoverflow.com/questions/19158339/why-are-global-variables-evil)" – Matthias Dec 13 '19 at 16:58
  • @Matthias Using global values is not a bad thing in my opinion, and it's a pretty easy way to solve this problem :) – Reznik Dec 13 '19 at 17:00
-1

When option 5 is entered you want to exit.


I added

import sys

and changed

elif x < 5:

to

elif x == 5:

and added

sys.exit(0)

I also added the getMenu() function

This is the complete code that is working in my editor:

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()

I also formatted your code (alt+Enter in Pycharm) to comply to PEP8 standards ;)

JaFizz
  • 328
  • 2
  • 20