【问题标题】:Python username and password with 3 attempts3次尝试的Python用户名和密码
【发布时间】:2018-04-22 11:24:56
【问题描述】:

刚开始使用 python 并为此绞尽脑汁,但似乎无法正确解决。

print('Enter correct username and password combo to continue')
count=0
password=Hytu76E
username=bank_admin

while password!='Hytu76E' and username!='bank_admin' and count<4:
    username=input('Enter username: ') and password=input('Enter password: ')

    if password=='Hytu76E' and username=='bank_admin':
     print('Access granted')

    else:
        print('Access denied. Try again.')
        count-=1

语法错误,无法分配给第 6 行 username=input 上的运算符。

【问题讨论】:

  • 格式化您的代码。你的 while 条件否定了其中的 if 条件。
  • count-=1改成count+=1然后去掉while循环中多余的用户名/密码检查
  • passwordusername 不应该在引号内吗?

标签: python loops while-loop


【解决方案1】:

你可以使用for循环:

#!/usr/bin/python3

for _ in range(3):
    usr = input("Enter username: ")
    psw = input("Enter password: ")
    
    if usr == "bank_admin" and psw == "Hytu76E":
        print("Access Granted!")
        break
    else:
        print("Access Denied!")
    print("Try Again!")
else:
    print("No more attemps!")

【讨论】:

    【解决方案2】:

    在这里试试这个(我尝试尽可能少地更改您的代码,以便您自己识别相同的逻辑)

    print('Enter correct username and password combo to continue')
    count = 0
    
    # "" or '' because you are assigning a value string into it
    password = ""
    username = ""
    
    # looping will continue when wrong input for three times and ask again...
    while password!='Hytu76E' and username!='bank_admin' and count < 3:
        # you are collecting user input from CLI separately (you can not assign and operator to such operation as per your code ;)
        username = input("Enter username: ")
        password = input("Enter password: ")
    
        if password=='Hytu76E' and username=='bank_admin':
         # if match, grand and break
         print('Access granted')
         break
    
        else:
            print('Access denied. Try again.')
            count+=1     # as per gbse, in the comments, you will need the + to count up
    

    代码中的问题:

    # you are assigning string value, what for? this would make the loop hit positive the first time
    password=Hytu76E       # string assignment error in syntax, anyway
    username=bank_admin    # string assignment error in syntax, anyway
    
    # you can not assigning and operator in the input because of no if condition in this line, also you should compare the values of the input
    username=input('Enter username: ') and password=input('Enter password: ')
    
    # if code is ok, then move outside the loop in the case when the user enters the first time good answers
    if password=='Hytu76E' and username=='bank_admin':
       print('Access granted')
    
        else:
            print('Access denied. Try again.')
    
            # you are decremented the counter which would never leave teh loop at 4, you should add one on each iteration so count+=1 (count = count + 1) 
            count-=1
    

    【讨论】:

      【解决方案3】:

      修复了代码以实现您想要做的事情:

      print('Enter correct username and password combo to continue')
      count=0
      while count < 3:
          username = input('Enter username: ')
          password = input('Enter password: ')
          if password=='Hytu76E' and username=='bank_admin':
              print('Access granted')
              break
          else:
              print('Access denied. Try again.')
              count += 1
      

      所做的更改:

      • 删除了usernamepassword的定义,因为它是多余的,可以省略
      • while 语句更改为计算count 的3 次迭代
      • 仅在if 语句中而不是在while 中验证凭据
      • count的递减改为递增(从count -=改为count +=
      • break 输入正确凭据时的循环

      【讨论】:

      • 这样更好,但你仍然有那些多余的任务。他们没有做任何有用的事情,他们只是浪费空间和时间。
      【解决方案4】:

      首先,您可以删除您在开始时为密码和用户名提供的初始定义,并将 while 循环更改为 while count

      所以它看起来像:

      print('enter the correct username and password combo to continue')
      count = 0
      while count<4:
      

      如果我们保持它以前的样子,那将是不必要的,并且会使您的程序更加混乱。

      要修复您的语法错误,您需要删除用户名和密码之间的 和,所以中间看起来更像这样:

      username = input('Enter username: ')
      password = input('Enter password: ')
      

      然后最后你想将 count-=1 更改为 count+=1,因为如果每次都取走一个,它永远不会达到 4,你的循环将是无限的,这不是你想要实现的。

      这是整个修复:

      print('Enter correct username and password combo to continue')
      count=0
      while count<4:
          username=input('Enter username: ')
          password=input('Enter password: ')
          if password=='Hytu76E' and username=='bank_admin':
              print('Access granted')
              count=5
          else:
              print('Access denied. Try again.')
              count+=1
      

      这是我所做的更改列表:

      • 删除了第 3 行和第 4 行中的密码和用户名定义

      • 将您的 while 循环更改为 while

      • 删除了 username=input 和 password=input 之间的和中间

      • 在 if 语句后添加 count=5 以便循环结束

      【讨论】:

        【解决方案5】:

        我认为这就是您要查找的内容:接受用户名和密码并根据代码中提到的特定密码进行验证,最大尝试限制为 3

        print('Enter correct username and password combo to continue')
        count=1
        
        while count<4:
            username=input('Enter username: ')
            password=input('Enter password: ')
            if password=='Hytu76E' and username=='bank_admin':
                print('Access granted')
                count=5
            else:
                print('Access denied. Try again.')
                count+=1
        

        【讨论】:

        • 这不会正确地重复重新评估输入。
        猜你喜欢
        • 2023-03-27
        • 1970-01-01
        • 2011-05-27
        • 2017-08-19
        • 2016-08-22
        • 2017-03-01
        • 2019-12-23
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多