Python Forum
looping and indentation issue
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
looping and indentation issue
#1
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.
Reply
#2
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
Reply
#3
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)
Reply
#4
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.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Bug Python Shell 3.9.0 - Issue with indentation Earis 17 6,597 Oct-31-2020, 07:00 AM
Last Post: Earis
  Infinite Looping Issue nsadams87xx 1 1,375 Jun-15-2020, 12:13 AM
Last Post: SheeppOSU
  PyPDF2 or indentation issue siten0308 3 4,596 Apr-20-2018, 09:35 PM
Last Post: siten0308
  Looping issue, stops working JohnOsis 7 4,422 Apr-05-2018, 09:20 PM
Last Post: nilamo
  Looping issue JohnOsis 2 2,917 Apr-01-2018, 08:56 AM
Last Post: JohnOsis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020