Python Forum

Full Version: looping and indentation issue
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi Guys,

I am new to python and have very basic knowledge on python. I have written a small program below which gives user 3 chances for writing correct user name and 3 chances for correct password. But the user should be able to proceed further in giving password only if his user name is correct else user should be asked to leave out. Also if the user does not enter incorrect password for more 3 times he should be asked to leave out. Also if some one can help me how to post the code as my indentation are not appearing in below code Below is my code:

[username = "John"
password = "John123"

attempt = 0
attempt1 = 0
while attempt <4:
u = input ("Please enter the username : ")
if u == username:
print ("Correct username")
p = input ("Please enter the password : ")
if p == password:
print("Access Granted")
else:
print ("incorrect password")
if attempt1 == 4:
print ("Too many attemps, Bye")
else:
input ("try again :")
attempt1 = (attempt1 + 1)
else:
print ("incorrect username.")
if attempt ==4:
print ("To many attempts, better luck next time")
else:
input ("Please try again: ")
attempt = (attempt + 1)
input ("Please enter to exit")
]

When i am executing the code and giving incorrect username, the chances i am getting is only 2 and not three.
Hello and welcome to Python and the forums.
First please put your code in Python code tags, you can find help here: https://python-forum.io/misc.php?action=help&hid=25
username = "John"
password = "John123"

attempt = 0
attempt1 = 0
while attempt <4:
                u = input ("Please enter the username : ")
                if u == username:
                                print ("Correct username")               
                                p = input ("Please enter the password : ")
                                if p == password:
                                                print("Access Granted")
                                else:
                                                print ("incorrect password")
                                                if attempt1 == 4:
                                                                                print ("Too many attemps, Bye")                               
                                                else:
                                                                               input ("try again :")
                                                attempt1 = (attempt1 + 1)
                else:
                                print ("incorrect username.")
                                if attempt ==4:
                                                print ("To many attempts, better luck next time")
                                else:
                                                input ("Please try again: ")
                                attempt = (attempt + 1)
If you need reuse code (you have to validate both username and password which are essentially the same tasks) one should use function. Define function which performs validation and call it twice - first for username and second for password.