如何使用我的代码(Python登录系统)?

卡尔马克迪

我是新编码员。这是我的第一个python登录系统代码。如何简化我的代码而不会丢失任何功能,例如错误的用户名和错误的密码等?

username = "zaphod"
password = "helloworld42"
username2 = "mozzie"
password2 = "mozzietheaussie"
userUsername = input("Hello, What is your username? \n")
UserPassword = input(print("Hello", userUsername, "What is your password? "))
if userUsername == username:
    if password == UserPassword:
        print("Hello", userUsername, "Welcome home")
if userUsername == username:
    if UserPassword != password:
        print("Wrong Password")
if userUsername == username2:
    if UserPassword != password2:
        print("Wrong Password")
if UserPassword == password:
    if userUsername != username:
        print("Wrong Username")
if UserPassword == password2:
    if userUsername != username2:
        print("Wrong Username")
if userUsername == username2:
    if UserPassword == password2:
        print("Hello", userUsername,"Welcome Home")
if userUsername == username:
    if UserPassword == password2:
        print("Are you gonna trick me pal xd")
if userUsername == username2:
    if UserPassword == password:
        print("Are you gonna trick me pal xd")
if userUsername != username:
    if userUsername != username2:
        if UserPassword != password:
            if UserPassword != password2:
                print("Wrong credidentals")
盖伊

您可以将凭据放在中,dict然后检查用户名是否存在并与密码匹配

credentials = {'zaphod': 'helloworld42',
           'mozzie': 'mozzietheaussie'}

user_name = input('Hello, what is your username?\n')
password = input(f'Hello {user_name}, what is your password?\n')

pas = credentials.get(user_name) # returns None if the user name doesn't exists
if not pas:
    print('Wrong Username')
elif pas != password:
    print('Wrong Password')
else:
    print(f'Hello {user_name}, welcome home')

print("Are you gonna trick me pal xd")似乎是不必要的,但是你可以通过修改添加它elif

elif pas != password:
    if password in credentials.values():
        print('Are you gonna trick me pal xd')
    else:
        print('Wrong Password')

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何使用Python登录网站?

如何将社交登录集成到我现有的用户架构/登录系统中?

如何使用Java代码登录网站?

我如何在使用python的终端中编写多行代码?

我如何通过python代码检测我的操作系统是32位还是64位

Python登录验证系统

如何修复此Python登录系统代码?

如何在模块中使用变量(登录系统)

我如何使用python硒读取html代码

Python:如何使用for循环执行我的代码5次?

我如何在Ubuntu中使用pyenv返回系统python

如何使用CSV文件简化Python中的登录代码错误?

当我的系统管理员拒绝让我更改它时,如何使用bash作为我的登录shell

使用PHP通过Facebook登录我的系统

如何使用登录系统打开文件?

在Android中,如何强制本机代码使用我的共享库版本而不是系统版本?

如何使用SAP JCO登录多个SAP系统

如何使用python代码在CentOS 6中重置系统代理

我的登录代码使我返回,而不是登录

当系统死机或崩溃重新登录时,我该如何调试?

Python 上的登录系统

在登录之前,如何在重新启动操作系统时从目录中删除几个文件?我使用的是 ubuntu 19.10?

如何在我的 Python 代码中使用 while 循环

我无法在我的代码中获得预期的输出(注册登录系统)

如何使用 Python 登录网站

我的 Python 代码如何越界?

如何使用我拥有的代码成功登录?

如何使我的 tkinter 登录系统正常工作?

登录系统 - Python