简体   繁体   中英

python program, want to print 5 attempts done, if used

I want to print 5 attempts completed if i use all the attempts incorrectly

def user_name_repeat():
    for repeat_user_name in range(4):
        re_enter_user_name = input("Incorrect username, try again: ")
        if re_enter_user_name == "vishesh":
            print("Correct user name ur logged in")
            break


input_ = input("enter user name: ")
if input_ == "vishesh":
    print("Correct user id, you are logged in: ")
elif input_ != "vishesh":
    print(user_name_repeat())

Why not wrap the whole thing inside a loop?

for _ in range(5):
    input_ = input("enter user name: ")
    if input_ == "vishesh":
        print("Correct user id, you are logged in")
        break
    else:
        print('Incorrect username, try again')
else:
    print('Attempts exhausted')

More info on for/else

Edit: You could also fix up the logic in your own code with something like-

def user_name_repeat():
    for repeat_user_name in range(4):
        re_enter_user_name = input("Incorrect username, try again: ")
        if re_enter_user_name == "vishesh":
            print("Correct user name ur logged in")
            return True
    return False


input_ = input("enter user name: ")
if input_ == "vishesh":
    print("Correct user id, you are logged in: ")
elif input_ != "vishesh":
    if not user_name_repeat():
        print('Attempts exhausted')

Really, all you need is to return an indicator (a boolean value in this case) from your function to let the caller know, whether the attempts succeeded.

Since you're working with a function, you should simply return the string instead of printing it inside of the function. That way, you can just return the "incorrect" string after the for loop.

def user_name_repeat():
  for repeat_user_name in range(4):           
    re_enter_user_name = input("Incorrect username, try again: ")
    if re_enter_user_name == "vishesh":
      return "Correct user name ur logged in"
  return "Number of incorrect tries exceeded limit"

Another approach would be using the else clause of the for loop. In loops, the code inside the else clause will only be executed if (and when) the loop ends "naturally"; that is - when the condition running the loop stops being valid (when the for reaches the end of the range(4) , for example).

In your code, the loop only ends if the limit of incorrect attempts is reached, since you break the loop if the user inputs the right password. So the following could be done:

def user_name_repeat():
  for repeat_user_name in range(4):           
    re_enter_user_name = input("Incorrect username, try again: ")
    if re_enter_user_name == "vishesh":
      print("Correct user name ur logged in")
      break
  else:
    print("Number of incorrect tries exceeded limit")

However, I should point out that the first option is way better, specially considering you're printing the return of the function call later (and if the function has no explicit return, you're actually printing None ).

You can even take the code out of the function, which should make things easier for you, as Chase's answer points out;).

Just use the else in your for loop, else will run after the loop is completed without encountering a break statement. Also, use return in your user_name_repeat() function, since you are calling it in print(user_name_repeat()) So it should return somethinig to the print() to print for:

def user_name_repeat():
    for repeat_user_name in range(4):
        re_enter_user_name = input("Incorrect username, try again: ")
        if re_enter_user_name == "vishesh":
            return("Correct user name ur logged in")
            break
    else:
        return "5 attempts completed"


input_ = input("enter user name: ")
if input_ == "vishesh":
    print("Correct user id, you are logged in: ")
elif input_ != "vishesh":
    print(user_name_repeat())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM