0

I'm creating a simple login program. It asks you for a username, then the password. If you type the correct password, you're in, else, you get a genial request to repeat your password. here is the code:

while True:
    turns= 5
    turns= turns-1
    print ("Username")
    L_username= input ("")
    print ("Authorising...")
    time.sleep(2)
    if(L_username)==("test@test.test"):
        for x in range (5):
            print ("Please enter the password")
            passwordlogin= input("")
            if passwordlogin == ("beta123"):
                print ("Hello, developer.")
                break
            break
            else:
                print ("Incorrect. You have",turns," more turns")

now the thing is, it gives me an error message: incorrect syntax for else: print ("incorrect... I know this is meant an issue because I wrote two 'breaks', the latter out of the for loop... which means it will break the while True loop at the start whether or not I go into the loop... which means the 'else' statement is out of the loop (sorry, I am not the best explainer at these things)... which gives the error message. But I don't understand how I should fix this problem... can anyone help?

I hope you understood me!

  • remove the second break, it's not syntactically correct because you wrote it at the same indentation as the if statement (which means the 'else' is syntactically wrong) – Pani Jul 14 '20 at 14:31
  • remove the second "break" statement. why are you using loop while you are using while statement, instead you can use condition in while loop itself. – Raady Jul 14 '20 at 14:32
  • Thanks for the help, everyone! I know this- I just wanted to ask, how do I break the 'while True' statement in the 'if statement' without causing the problem you mentioned? Thanks for the help! – Sandeep Srinivasan Jul 14 '20 at 14:38

3 Answers3

1

This could be triviallized by using a function and returning from it once you reach that if statement.

def func():
    while True:
        turns= 5
        turns= turns-1
        print ("Username")
        L_username= input ("")
        print ("Authorising...")
        time.sleep(2)
        if(L_username)==("test@test.test"):
            for x in range (5):
                print ("Please enter the password")
                passwordlogin= input("")
                if passwordlogin == ("beta123"):
                    print ("Hello, developer.")
                    return
                else:
                    print ("Incorrect. You have",turns," more turns")

However, if you insist on not having a function, you can basically have a flag in your code that you set to true inside that if block. Before continuing the outer loop, you should check if this flag is set to True, if it is, you can safely break.

while True:
    success = False
    turns= 5
    turns= turns-1
    print ("Username")
    L_username= input ("")
    print ("Authorising...")
    time.sleep(2)
    if(L_username)==("test@test.test"):
        for x in range (5):
            print ("Please enter the password")
            passwordlogin= input("")
            if passwordlogin == ("beta123"):
                print ("Hello, developer.")
                success = True
                break
            else:
                print ("Incorrect. You have",turns," more turns")
    if success:
        break

Note should be taken for nested loops and such intense nesting for what is essentially a trivial problem. Please try to use a singular loop with your conditions.

Chase
  • 5,315
  • 2
  • 15
  • 41
0

Well I have written your concept in my own way. Hope this works for you

turns = 5

while turns != 0:
    username = input('Enter Username: ')
    password = input(f'Enter password for {username}: ')

    if username == 'test@test.test' and password == 'beta123':
        print('Hello developer')
        turns = 0    # Breaking while loop if user get access within number of "turns"

    else:
         turns = turns - 1  # Or turns -= 1
         print('Incorrect username or password')
         print(f'You have {turns} turns for gaining access')
Ghantey
  • 626
  • 2
  • 11
  • 25
  • Thanks... The issue is, if I carry out the commands that you have written, even if I do get all the info correct, it will make me write the info again – Sandeep Srinivasan Jul 14 '20 at 14:44
  • You are welcome but could not get the other part of your saying. – Ghantey Jul 14 '20 at 14:49
  • so... the 'if passwordlogin == ("beta123"): print ("Hello, developer.") break break' command, as you correctly point out, is incorrect. However, I'm asking you why it still asks me the username if I've written everything out. – Sandeep Srinivasan Jul 14 '20 at 14:51
  • nope it won't ask you to write the info again if your login gets successful once – Ghantey Jul 14 '20 at 14:57
0

You need to swap the else and break and change the break's indentation. If the user gets the correct username you want to test the password at most 5 times and then quit

while True:
turns= 5
print ("Username")
L_username= input ("")
print ("Authorising...")
time.sleep(2)
if(L_username)==("test@test.test"):
    for x in range (5):
        print ("Please enter the password")
        passwordlogin= input("")
        if passwordlogin == ("beta123"):
            print ("Hello, developer.")
            break
        else:
            turns -= 1
            print ("Incorrect. You have",turns," more turns")
     break

So in this case you run the for loop and then break

mattyx17
  • 806
  • 6
  • 11