0

I'm working on a password protected calculator. I'm having trouble with how I can ask the user if they want to continue or not. In case they want to continue, the calculator will show up again. The 'Y' condition is working perfectly but 'N' condition is also displaying the calculator again.

count=0
while count < 3:
    password = input('Enter password: ')
    if password=='HELLO123':
        print("Access Granted")
        print("Welcome to calculator program")
        number_1 = int(input('Enter your first number: '))
        number_2 = int(input('Enter your second number: '))
        print('{} + {} = '.format(number_1, number_2))
        print(number_1 + number_2)
        print('{} - {} = '.format(number_1, number_2))
        print(number_1 - number_2)
        print('{} * {} = '.format(number_1, number_2))
        print(number_1 * number_2)
        print('{} / {} = '.format(number_1, number_2))
        print(number_1 / number_2)
        while input("Do You Want To Continue? [y/n]") == "y" or "Y":
          print("Welcome to calculator program")
          number_1 = int(input('Enter your first number: '))
          number_2 = int(input('Enter your second number: '))
          print('{} + {} = '.format(number_1, number_2))
          print(number_1 + number_2)
          print('{} - {} = '.format(number_1, number_2))
          print(number_1 - number_2)
          print('{} * {} = '.format(number_1, number_2))
          print(number_1 * number_2)
          print('{} / {} = '.format(number_1, number_2))
          print(number_1 / number_2)
          break
    break
else:
        print('Access denied. Try again.')
        count += 1
Ch3steR
  • 20,090
  • 4
  • 28
  • 58

2 Answers2

2

Your problem is with this line:

while input("Do You Want To Continue? [y/n]") == "y" or "Y":

The expression input("Do You Want To Continue? [y/n]") == "y" or "Y" is always True, because it is the same as (input("Do You Want To Continue? [y/n]") == "y") or "Y". Since "Y" is not an empty string, it evaluates to True and <anything> or True evaluates to True.

Note that it also wouldn't work if the or bound more strongly than the ==, since input("Do You Want To Continue? [y/n]") == ("y" or "Y") is the same as input("Do You Want To Continue? [y/n]") == True and that would always be True until you enter an empty string.

What you're looking for is:

while input("Do You Want To Continue? [y/n]") in ["y", "Y"]:

I wouldn't recommend coding your problem like this, but it will solve your immediate problem.

Grismar
  • 27,561
  • 4
  • 31
  • 54
1

Your issue is in the logic of your while loop. while x == a or b: will test if x == a or if b. In your case, this means that your second test is "y", which evaluates to True, and as such your while loop never ends!

In order to check if the input() result is one of many potential results, you could use an in comparison to compare the result against an iterable such as a tuple:

while input("Do You Want To Continue? [y/n]") in ("y", "Y"):
    print("Welcome to calculator program")
Oliver.R
  • 1,282
  • 7
  • 17