BigTallJosh
BigTallJosh

Reputation: 866

While loop issues in Python

I'm trying to write a small Python program for use in secondary schools to teach about ciphers.

It's all part of an ongoing project they have to do.

However on my while loops I have an if condition inside them and if the condition isn't met the first time it just infinitely loops until I break.

while esc == False:
    if guess != cipher:
        print("ERROR: INCORRECT CIPHER:" + Decrypt(guess, enc))
        pass
    else:
        esc = True
        print("Correct Cipher! Decryption successful:" + Decrypt(guess, enc))

The cipher here is 12 and if that is input it carries on as normal, however any other input just gives the error message and loops out forever.

I'm from a C#/C++ background usually and I know Python needs to be in line with its tab characters and white space and I've checked that a few times but I'm at a loss now.

ADDITIONAL CODE:

This works fine and It's done the same way

while esc == False:
    if CheckPassword(pw) == True:
        print("Authorisation successful.")
        esc = True
    else:
        pw = input("Password =  :  ")

ADDITIONAL PLACES WHERE SAME PROBLEM HAPPENS:

while esc == False:
    if attempts < 1:
        esc = True
        GameOver()
    elif x == "USERS":
        print("USERS UNAVAILABLE.")
        attempts = RemoveAttempt(attempts)
        pass

Upvotes: 1

Views: 166

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599956

I'm not sure what else you are expecting to happen. If the guess is incorrect once you get into the while loop, it is going to always be incorrect, because you never ask the user for another guess. Note that in the one that does work, the else clause has an input for a new password: you don't do that in either of your non-working examples.

(Also I think you might be confused about what pass does: it's simply a no-op, ie it does nothing at all, and is only useful where you syntactically need a statement but don't want to do anything. That's not the case in either of the places you use it, so you should remove it. Unless you perhaps meant break instead, to break out of the loop?)

Upvotes: 5

Related Questions