python 使用 while 循环进行有限试用的简单密码登录
python simple password login with limited trial with while loop
Z=1 #username can be ramdom as long as password is cyber
while (Z>0):
Z=Z+1
username=raw_input('key in your username: ')
if username==(""):
print '\nPlease do not leave it blank! \nPlease type again... \n'
else:
Z=0
else:
Z=Z+1
P=0 #password part
while (P<3):
pssw=raw_input('key in your password: ')
if pssw=="cyber":
P=3
else:
pssw=raw_input('\nWrong password, \nSecond chance: ')
P=P+1
if pssw=="cyber":
P=3
else:
pssw=raw_input('\nWrong password, \nWrong again will terminate the program... \nType again: ')
P=P+1
if pssw=="cyber":
P=3
else:
print "Bye..."
exit('ASGN_1151102784_A01A.py') #terminate program after 3 error pssw
print "\nWelcome "+username
#to be continue...
以上是我的登录代码,它需要任何随机用户名,但条件是密码必须是网络密码。但是,我的讲师拒绝接受此代码,因为她说密码检查系统应在 while 循环内完成,而不是在 while 循环内创建新的 if else 语句。
现在我真的很困惑。是否有任何解决方案可以通过 3 次试用向用户请求密码,如果密码正确,则继续 运行 程序,否则仅使用简单的 while 循环退出程序?
可能是这样的:
...
P=0 #password part
pssw = None
messages = {
0: 'key in your password: ',
1: '\nWrong password, \nSecond chance: ',
2: '\nWrong password, \nWrong again will terminate the program... \nType again: '
}
try:
while (pssw != 'cyber'):
pssw=raw_input(messages[P])
P += 1
except KeyError:
print "Bye..."
exit('ASGN_1151102784_A01A.py') #terminate program after 3 error pssw
else:
print "\nWelcome "+username
Z=1 #username can be ramdom as long as password is cyber
while (Z>0):
Z=Z+1
username=raw_input('key in your username: ')
if username==(""):
print '\nPlease do not leave it blank! \nPlease type again... \n'
else:
Z=0
else:
Z=Z+1
P=0 #password part
while (P<3):
pssw=raw_input('key in your password: ')
if pssw=="cyber":
P=3
else:
pssw=raw_input('\nWrong password, \nSecond chance: ')
P=P+1
if pssw=="cyber":
P=3
else:
pssw=raw_input('\nWrong password, \nWrong again will terminate the program... \nType again: ')
P=P+1
if pssw=="cyber":
P=3
else:
print "Bye..."
exit('ASGN_1151102784_A01A.py') #terminate program after 3 error pssw
print "\nWelcome "+username
#to be continue...
以上是我的登录代码,它需要任何随机用户名,但条件是密码必须是网络密码。但是,我的讲师拒绝接受此代码,因为她说密码检查系统应在 while 循环内完成,而不是在 while 循环内创建新的 if else 语句。
现在我真的很困惑。是否有任何解决方案可以通过 3 次试用向用户请求密码,如果密码正确,则继续 运行 程序,否则仅使用简单的 while 循环退出程序?
可能是这样的:
...
P=0 #password part
pssw = None
messages = {
0: 'key in your password: ',
1: '\nWrong password, \nSecond chance: ',
2: '\nWrong password, \nWrong again will terminate the program... \nType again: '
}
try:
while (pssw != 'cyber'):
pssw=raw_input(messages[P])
P += 1
except KeyError:
print "Bye..."
exit('ASGN_1151102784_A01A.py') #terminate program after 3 error pssw
else:
print "\nWelcome "+username