简体   繁体   English

Python登录系统

[英]Login system on Python

This is a really newbie question. 这是一个真正的新手问题。 So, I am trying to code a login system in Python, where it asks for an username (only 1 username available), if the username typed in is incorrect, it says the username is invalid, if it is correct, it asks for a password, if the password is incorrect, it says incorrect password and proceeds to ask for the password once again, and if the password typed in is correct, it just says logged in. 因此,我正在尝试在Python中编写一个登录系统,要求输入用户名(仅提供1个用户名),如果键入的用户名不正确,则说明用户名无效,如果正确,则要求输入用户名。密码,如果密码不正确,则表示密码错误,然后再次要求输入密码;如果输入的密码正确,则仅表示已登录。

What I've been able to do so far is: 到目前为止,我能够做的是:

a = 0

 while a < 1:             
     print ('Username:')  
     name = input()
     if name != 'Teodor': #checks the username typed in
         print ('Invalid username.') 
         continue
     else:
         print ('Hello, Teodor.')
         print ('Password:')
         a = a + 1

 password = input()

 b = 0
      while b < 1:
     if password != '1234': #checks the password typed in
         print ('Password incorrect.')
         b = b + 1
         continue

     else:
         print ('Password correct.')
         print ('Logging in...')
         print ('Logged in.')
         break

This works, although it does something I don't want if the user types in the incorrect password. 尽管用户输入了错误的密码,但它确实执行了我不希望执行的操作,但它确实可行。 If the user types in the incorrect password, I wanted the program to tell the user 'Incorrect password' and proceed to ask for it once again, but it doesn't do that, it just prints 'Incorrect password', and then it terminates. 如果用户输入了错误的密码,我希望程序告诉用户“错误的密码”并再次要求输入,但是它没有这样做,它只会打印“错误的密码”,然后终止。 It works 100% on the part where it asks for the username, though. 不过,它在要求用户名的那部分工作100%。

It's a little thing I am missing. 这是我想念的一件事。 How can I fix this? 我怎样才能解决这个问题? Thanks a lot! 非常感谢!

The statement b = b + 1 is terminating your while loop every time the user enters an incorrect password. 每当用户输入错误的密码时,语句b = b + 1就会终止while循环。 There is really no need for it. 确实没有必要。

You can alternatively just wrap your password prompt in a while loop: 您也可以将密码提示包装在while循环中:

while input("Enter password") != "1234":
    print("Password incorrect")

There is no need for the + 1 when checking for the password. 检查密码时不需要+ 1 That just breaks you out of the loop. 那只是使您脱离循环。

Instead, try: 相反,请尝试:

if password != '1234': #checks the password typed in
         print ('Password incorrect.')
         continue

A much better solution, instead of using +1 and <1 to break out of loops is to use booleans. 一个更好的解决方案是使用布尔值,而不是使用+1<1来打破循环。 Sample: 样品:

userCorrect = False
while not userCorrect:
    print ('Username:')
    name = raw_input()
    if name != 'Teodor': #checks the username typed in
        print ('Invalid username.')
        continue
    else:
        print ('Hello, Teodor.')
        print ('Password:')
        userCorrect = True

password = raw_input()

passCorrect = False
while not passCorrect:
    if password != '1234': #checks the password typed in
        print ('Password incorrect.')
        print ('Password:')
        password = raw_input()
    else:
        passCorrect = True
# Since password is correct, continue.
print ('Password correct.')
print ('Logging in...')
print ('Logged in.')

This loop ( while b < 1: ) terminates the moment an invalid password is entered. 输入无效密码后,此循环( while b < 1: )终止。

Look at 看着

>     if password != '1234': #checks the password typed in
>         print ('Password incorrect.')
>         b = b + 1
>         continue

The line of code b = b + 1 makes it such that while b < 1: becomes false, thus ending the loop and terminating your program. 代码行b = b + 1使得while b < 1:时为假,从而结束了循环并终止了程序。

As others have already pointed out, the problem lies is b = b + 1 breaking the condition while b < 1: , causing it not to ask for another password. 正如其他人已经指出,问题出在b = b + 1打破了条件while b < 1:使其不要问另一个密码。 Simple delete the line b = b + 1 简单删除行b = b + 1

Want to make it better? 是否想做得更好?

Avoid the 'over-the-shoulder' attack with getpass() instead of input() . 使用getpass()而不是input()避免“过肩”攻击。 Your password input is masked as **** 您的密码输入被屏蔽为****
ex. 恩。

from getpass import getpass
password = getpass()

Cryptify Cryptify
Well, apart from sounding cool, this doesn't really stop somebody from modifying the code to skip past the password phase -but it can stop them from seeing the raw password in the code. 嗯,除了听起来很酷之外,这并没有真正阻止某些人修改代码以跳过密码阶段,但是可以阻止他们看到代码中的原始密码。

this post has a good example using passlib 这篇文章使用passlib有一个很好的例子

This helps protect non-unique/sensitive passwords (like the password you use for 5x other things, or your mother's maiden name... lets not drag her into this) 这有助于保护非唯一/敏感的密码(例如您用于5倍其他密码的密码,或者您母亲的娘家姓……请勿将其拖入其中)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM