尝试 3 次的 Python 用户名和密码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47202331/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python username and password with 3 attempts
提问by Laura_
Just started python and racking my brains on this but can't seem to get it right.
刚刚开始使用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
syntax error, can't assign to operator on line 6 username=input.
语法错误,无法分配给第 6 行 username=input 的运算符。
回答by Megabeets
Fixed the code to achieve what you are trying to do:
修复了代码以实现您要执行的操作:
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
Changes that have been made:
已作出的更改:
- Removed the definition of
username
andpassword
since it is redundant and can be omitted - Changed the
while
statement to count 3 iterations ofcount
- Validation of the credentials only in the
if
statement and not in thewhile
- Changed the decreasing of
count
to increasing(fromcount -=
tocount +=
) break
the loop when the right credentials are entered
- 删除了
username
and的定义,password
因为它是多余的,可以省略 - 将
while
语句更改为计算 3 次迭代count
- 仅在
if
声明中而不是在声明中验证凭据while
- 将减少更改
count
为增加(从count -=
到count +=
) break
输入正确凭据时的循环
回答by oetoni
here try this (I try to change your code as less as possible so that you can identify the same logic yourself)
在这里尝试这个(我尝试尽可能少地更改您的代码,以便您可以自己识别相同的逻辑)
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
issues in your code:
您的代码中的问题:
# 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
回答by Van Peer
I think this is what you're looking for: Accept username and password and verify it against a particular one mentioned in the code, with a max try limit of 3
我认为这就是您要查找的内容:接受用户名和密码并根据代码中提到的特定密码进行验证,最大尝试限制为 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
回答by Blazeoffury49er
Firstly you can remove the initial definition you gave to password and username at the start as well as changing the while loop to become while count<4
首先,您可以删除在开始时给密码和用户名的初始定义,并将 while 循环更改为 while count<4
So it would look like:
所以它看起来像:
print('enter the correct username and password combo to continue')
count = 0
while count<4:
If we had kept it how it was previously it would be unnecessary and clutter your program more.
如果我们保持它以前的样子,那将是不必要的,并且会使您的程序更加混乱。
To fix your syntax error you need to remove the and placed inbetween username and password, so the middle will look more like this:
要修复您的语法错误,您需要删除 和 放置在用户名和密码之间,因此中间看起来更像这样:
username = input('Enter username: ')
password = input('Enter password: ')
Then at the end you want to change count-=1 to count+=1, because if it takes one away every time it will never hit 4 and your loop will be infinite, which is not what you are trying to achieve.
然后最后您想将 count-=1 更改为 count+=1,因为如果每次都拿走一个,它将永远不会达到 4 并且您的循环将是无限的,这不是您想要实现的。
Here is the entire fix:
这是整个修复:
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
Here is a list of changes I have made:
这是我所做的更改列表:
Removed the password and username definition in lines 3 and 4
Changed your while loop to become while<4
Removed the and inbetween username=input and password=input
Added count=5 after if statement so that the loop ends
删除了第 3 行和第 4 行中的密码和用户名定义
将您的 while 循环更改为 while<4
删除了用户名=输入和密码=输入之间的和
在 if 语句后添加 count=5 以便循环结束