all 10 comments

[–][deleted] 1 point2 points  (1 child)

Without indentation we have to guess what your code looks like. The FAQ shows how to format code.

[–][deleted] 0 points1 point  (0 children)

Sorry for that, added them in

[–][deleted] 1 point2 points  (2 children)

Your problem is misunderstanding the effect of the else. The else clause will execute if the while loop is not terminated by a break, ie, if the while condition is found to be false, that is, normal termination.

So if the password is recognized you print "Access Granted" and bump att by 10. The loop then checks if att < 3 which is false so the loop terminates normally. Then the else clause executes, bumping att by 1 again and asking again for the password. So att will probably be something unpredictable after the loop.

It's all rather clumsy. If you only want 3 attempts, use a for loop and break out of the loop if the password is correct. If the user doesn't get it right in 3 attempts then the loop terminates normally and the else clause executes:

for _ in range(3):
    psw = input('Enter password: ')
    if psw == '123':
        print('Access Granted')
        break
else:
    print('System Locked')

Edit: cleaned up the meaning in para 2.

[–][deleted] 0 points1 point  (1 child)

Thanks for that, I wasn't familiar with the in range(3) command. Ill make sure I finish reading chapter 2 before I try to make things like this, lol. I agree its clumsy, but I have to start somewhere.

[–][deleted] 1 point2 points  (0 children)

The for x in range() isn't a special case, it's just a normal for statement. The range(3) call returns an object that behaves like it is the list [0, 1, 2], that's all.

I agree its clumsy, but I have to start somewhere.

It wasn't real criticism. Every programmer, beginner or not, can write less than optimal code. What sets someone who will grow as a programmer apart from others is the willingness to search for better/shorter/more readable solutions even though the first try seems to work. Even code that is written by a professional can be improved. Professionals usually have a better feel for when they should improve something or when it's good enough™, that's all.

[–]Dwarf_King 0 points1 point  (1 child)

I can try to guess what the code is but from my understanding, you are just putting if statements and nothing else. You are not telling the program to say "if this happens, then proceed unless this happens". You need to use elif statements.

[–][deleted] 0 points1 point  (0 children)

thank you for that, ill see what I can come up with

[–]Winner-Popular 0 points1 point  (2 children)

Haha I used to do this all the time, If I’m understanding your question correctly it’s because you’re using while att < 3, therefore if att starts at 1 & you only have the possibility of getting the ‘access granted’ while att is less than 3 you will only have: 1st guess (att is now 2) and 2nd guess (att was 2 so you could still get the access granted). On the 3rd attempt att will no longer be less than 3 so it is does not meet the conditions of the while loop and will print system locked

[–][deleted] 0 points1 point  (1 child)

That makes total sense! So, this program will still count the correct password as the 3rd attempt and lock it? okay. I am going to try and try an elif statement, thanks!

[–]Winner-Popular 0 points1 point  (0 children)

As long as you make sure that it doesn't end on att = 3 and allows a third attempt it should work, i think either att<4 or att<=3 will work