Python Forum
Looping over amount of attempts
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Looping over amount of attempts
#1
Hey so my code is about a username and password system. My issue is that after the user fails to input the correct details (from Intro) more than once it appears with the same message. What should be happening is that when attempt = 2, it should say you have 1 more attempt. If they entered the wrong details again, the message you have entered 3 times and you are now locked out, contact the network office. This is part is in Username_Submit(name). Doesn't appear with an actual error, I think it justs loops over.

import time


def intro(): #Username and Password is displayed
    print("Hello, your username is: Greg232 ")
    time.sleep(1)
    print("Your password is: userword657")
    Username_verify()

def Username_verify():
    name = Username_Name()
    proceed = Username_Submit(name)#Prints the credientals the user has put in
    print(proceed)


def Username_Name(): #User must input the correct username and password given to them
    name=list()
    username = input('Username: ')   
    double_check = input('Are you sure? ')
    if double_check != 'yes':
          Username_Name()     #If they say anything other than yes they will need to input their username again
    if double_check == 'yes':
            print('Proceed...')
            name.append(username)
            
    password = input("Password: ")
    double_check = input('Are you sure? ') 
    if double_check != 'yes':
        return double_check
        name.remove(Username_Name())
    if double_check == 'yes':
        name.append(password) #If the user says yes then Username_Submit(name) will check they match with the
                              #correct credientials 
    if Username_Submit(name) != 'Greg232' and  'userword657':
        name.remove(Username_Name())
        

    return name


def Username_Submit(name):
    attempt = 1 
    print(name[0], name[1])

    Submit = input("Submit?") #Double checks that the user is ok with what they entered
    if Submit == 'yes':
         if name[0] == 'Greg232' and name[1] == 'userword657':
             print("You may enter the system...")#User inputs correct username and password 
         else:                                   #therefore they can enter the system
             while name[0] != 'Greg232' and name[1] != 'userword657' and attempt == 1:
                 attempt = attempt + 1 #Username or/and password is not correct, they have 2 more attempts
                 print("Your username or password is wrong.")
                 print("You have 2 more attempts left. ")
                 Username_Name()
                 print(name[0], name[1])
                 Submit = input("Submit?")
                 if Submit == 'yes':
                     if name[0] == "Greg232" and name[1] == "userword657" and attempt == 1:
                      print("You may enter the system...")#User can retry typing in the correct details 
                      
                     
                     elif name[0] != 'Greg232' and name[1] != 'userword657' and attempt == 2:
                         attempt = attempt + 1
                         print("You have 1 more attempt left.")
                         Username_Name()    #Everytime the user inputs the wrong log-in details,
                                        #a message will appear with the amount of attempts remaining
                     elif name[0] != 'Greg232' and name[1] != 'userword657' and attempt == 3:
                         print("You have entered 3 attempts and you are now locked out,")
                         print("contact the network office.")
                         
    else:
         Username_Name() #If user does not say yes they must re-enter their details 
         



intro()
Reply
#2
I find it really hard to follow the flow of your code as it seems pretty convoluted.

I'd expect to see an overall while loop or for loop that counts up or down the number of overall attempts and tells them to get help if they fail. Good practice is not to indicate whether the error is in the username or password as you don't want the system to help people guess and confirm usernames.

I'd expect to see an error message that prints something different on the 2nd and 3rd attempt, but uses the same code.

Here's some code to give you an idea of what I'm talking about.

def dosomething():
    return False

def main():
    errmsg = ['2nd chance', 'last chance', 'Sorry - you need help']
    maxtries = len(errmsg)-1
    for trying in range(maxtries):
        if dosomething():
            break
        print(errmsg[trying])
    else:
        print(errmsg[maxtries])
        return False
    # rest of your code
    return True

main()
Incidentally, in Python, it is coming to use a while True loop to continue to repeatedly do something until an exit condition is achieved, similar to the for loop in the example above:

while True:
    name = input('What is your name: (enter to end): ')
    if not name:
        break
    print(f'Hello {name}')
I am trying to help you, really, even if it doesn't always seem that way
Reply
#3
So when the user fails for the 2nd time, line 64 should be true saying you have 1 more attempt. What is happening is it is still saying you have 2 more attempts from line 52. The user gets 3 attempts to get it right

Submit = input("Submit?") #Double checks that the user is ok with what they entered
    if Submit == 'yes':
         if name[0] == 'Greg232' and name[1] == 'userword657':
             print("You may enter the system...")#User inputs correct username and password 
         else:                                   #therefore they can enter the system
             while name[0] != 'Greg232' and name[1] != 'userword657':
                  #Username or/and password is not correct, they have 2 more attempts
                 if attempt == 1:
                     attempt = attempt + 1 
                     print("Your username or password is wrong.")
                     print("You have 2 more attempts left. ")
                     Username_Name()
                     print(name[0], name[1])
                     Submit = input("Submit?")
                     if Submit == 'yes':
                         if name[0] == "Greg232" and name[1] == "userword657" and attempt == 1:
                          print("You may enter the system...")#User can retry typing in the correct details 
                      
                     
                         elif name[0] != "Greg232" and name[1] != "userword657" and attempt == 2:
                             attempt = attempt + 1
                             print("You have 1 more attempt left.")
                             Username_Name()    #Everytime the user inputs the wrong log-in details,
                                                #a message will appear with the amount of attempts remaining
                         elif name[0] != 'Greg232' and name[1] != 'userword657' and attempt == 3:
                             print("You have entered 3 attempts and you are locked out,")
                             print("contact the network office.")
                         
Reply
#4
What have you changed between the first and second post of your code?
I am trying to help you, really, even if it doesn't always seem that way
Reply
#5
Nothing just used the part of the code that is going wrong to help illustrate what I mean.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Print the frequency of each coin for the combinations that sum to the amount N Pranav 3 2,540 May-19-2020, 06:16 AM
Last Post: Pranav
  Unexpected change to a list in a small amount of self-contained code Johno 5 2,863 Mar-15-2020, 05:06 PM
Last Post: jefsummers
  Program that displays the number with the greatest amount of factors ilusmd 3 2,823 Nov-01-2018, 08:28 PM
Last Post: ichabod801
  Guessing Game "limit 4 attempts" help rprollin 3 19,727 Jun-23-2018, 04:37 PM
Last Post: ljmetzger

Forum Jump:

User Panel Messages

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