python实现用户输入用户名和密码不能为空_Python:匹配用户名w/password,不正确时提示输入密码;...

我正在尝试创建一个登录名。在

我不知道如何创建/导入一个用户名和密码库;我正在研究寻找答案,但我问了两种方法。在

将用户名与密码匹配(部分解决;需要添加多个具有匹配密码的用户名)

如果密码不正确,如何创建循环?如果输入的密码不正确,则需要再次提示用户输入密码。/(已解决;使用[print]而不是[return])

如何将循环限制为密码尝试的特定次数。在

以下是我尝试过的:def check_password(user, password):

""" Return True if the user/pass combo is valid and False otherwise. """

# Code to lookup users and passwords goes here. Since the question

# was only about how to do a while loop, we have hardcoded usernames

# and passwords.

return user == "pi" and password == "123"

def login():

“”“反复提示输入用户名和密码,直到它正常工作为止。

仅当成功时返回True。

“”

^{pr2}$

用于测试

登录()

这修复了由于使用“return”而不是“print”和“if”而不是“while”导致的“if error password.*缺少循环提示def login():

#create login that knows all available user names and match to password ; if password is incorect returns try again and propmts for password again#

username = raw_input('username:')

if username !='pi':

#here is where I would need to import library of users and only accept those usernames; needs to be like 'pi' or 'bob' or 'tim'etc.

print'user not found'

username = raw_input('username')

password = raw_input('password:')

#how to match password with user? store in library ?

while password != '123':

print 'please try again' # You have to change the 'return' to 'print' here

password = raw_input('password')

return 'access granted'

#basically need to create loop saying 'try again' and prompting for password again; maybe smarter to ask limited number of

#times before returning 'you have reached limit of attempts#

if password == '123':

#again matching of passwords and users is required somehow

return 'access granted'login()

username:wronguser

user not found

usernamepi

password:wrongpass

please try again

password123

'access granted'

#更新前的首次尝试感谢:Merigrim:

def login():# Create login that knows all available user names and match to password;

# if password is incorect returns try again and propmts for password again#

username = raw_input('username:')

if username !='pi':

# Here is where I would need to import library of users and only

# accept those usernames; needs to be like 'pi' or 'bob' or 'tim'etc.

return 'user not found'

password = raw_input('password:')

# How to match password with user? store in library?

if password != '123':

return 'please try again'

password = raw_input('password:')

if password != '123':

return 'please try again'

# Basically need to create loop saying 'try again' and prompting

# for password again; maybe smarter to ask limited number of

# times before returning 'you have reached limit of attempts

elif password == '123':

# Again matching of passwords and users is required somehow

return 'access granted'

以下是它目前的工作方式:>>> login()

username:pi

password:123

'access granted'

>>> login()

username:pi

password:wrongpass

'please try again'

我需要创建循环来再次提示输入密码。在

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值