简体   繁体   English

python 程序,想打印 5 次尝试完成,如果使用

[英]python program, want to print 5 attempts done, if used

I want to print 5 attempts completed if i use all the attempts incorrectly如果我不正确地使用所有尝试,我想打印 5 次尝试完成

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有关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.实际上,您只需要从 function 中返回一个指示符(在这种情况下为 boolean 值),以让调用者知道尝试是否成功。

Since you're working with a function, you should simply return the string instead of printing it inside of the function.由于您使用的是 function,因此您应该简单地返回字符串,而不是将其打印在 function 中。 That way, you can just return the "incorrect" string after the for loop.这样,您可以在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"
  return "Number of incorrect tries exceeded limit"

Another approach would be using the else clause of the for loop.另一种方法是使用for循环的else子句。 In loops, the code inside the else clause will only be executed if (and when) the loop ends "naturally";在循环中, else子句中的代码只有在(以及何时)循环“自然”结束时才会执行; that is - when the condition running the loop stops being valid (when the for reaches the end of the range(4) , for example).也就是说 - 当运行循环的条件停止有效时(例如,当for到达range(4)的末尾时)。

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.在您的代码中,循环仅在达到错误尝试的限制时才会结束,因为如果用户输入了正确的密码,您就会break循环。 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 ).但是,我应该指出,第一个选项要好得多,特别是考虑到您稍后要打印 function 调用的返回(如果 function 没有明确的返回,您实际上是在打印None )。

You can even take the code out of the function, which should make things easier for you, as Chase's answer points out;).您甚至可以从 function 中取出代码,正如 Chase 的回答所指出的那样,这应该会让您更轻松;)。

Just use the else in your for loop, else will run after the loop is completed without encountering a break statement.只需在for循环中使用elseelse将在循环完成后运行,而不会遇到break语句。 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:此外,在您的user_name_repeat() function 中使用return ,因为您在print(user_name_repeat())中调用它所以它应该返回一些东西给print()以打印:

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())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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