只有用户可以输入程序,但是其他用户无法输入程序
我已经对代码进行了更改,以便该程序通过包含该程序的用户名和密码的整个文件运行,但是我得到只有用户名“管理员”才能访问该程序的反馈,而不是其他用户。
是否有其他方式可以通过整个文件运行程序并查看输入的用户名是否与文件中的用户名相同?
我的代码如下:
def login():
username = input("Enter your username: ")
password = input("Enter your password: ")
for line in open('user.txt', 'r').readlines():
field = line.strip().split(", ")
if username == field[0] and password == field[1]:
print('Welcome' + username)
return True, field[0] == "admin"
return False, False
login_success,is_admin = login()
如果login_success和is_admin: display_admin_menu_options() elif login_success: display_menu_options() 别的: 打印(“用户名或密码不正确!”)
I have made changes to my code so that the program runs through the whole file containing the usernames and passwords for the program, but I get feedback that only the username 'admin' can have access to the program, and not other users.
Is there another way where the program can maybe run through the whole file and see if the username entered is the same as the username in the file?
My code is as follows:
def login():
username = input("Enter your username: ")
password = input("Enter your password: ")
for line in open('user.txt', 'r').readlines():
field = line.strip().split(", ")
if username == field[0] and password == field[1]:
print('Welcome' + username)
return True, field[0] == "admin"
return False, False
login_success, is_admin = login()
if login_success and is_admin:
display_admin_menu_options()
elif login_success:
display_menu_options()
else:
print("Username or password incorrect!")
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的外部返回语句位于前面的内部。尝试:
我的猜测是
admin
在user.txt
文件的顶部。因此,一旦您输入管理员密码和用户名,它就与字段匹配,然后返回true,true
。但是,如果您输入其他内容,它会跳过IF语句并返回false,false
。您永远不会循环一次以上,因为您的返回语句总是在第一个循环中终止程序。Your outer return statement is inside the for-loop. Try:
My guess is that
admin
is at the top ofuser.txt
file. So as soon as you input the admin password and username, it matches the fields and you returnTrue,True
. But if you input something else, it skips the if statement and returnsFalse,False
. You never loop more than once because your return statement always terminates the program in the first loop.