微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何更正此python登录系统?

如何解决如何更正此python登录系统?

我对编程真的很陌生,我得到了一个关于使用文本文件创建登录系统的练习。问题是我对我的代码感到非常困惑,登录系统包含两个角色,即管理员和客户,客户分为注册客户和未注册客户。管理员注册客户应该直接登录系统,而未注册客户则需要创建一个新帐户。此外,我们不允许使用全局变量或导入。如果代码绝对混乱,我深表歉意。

这是我的代码

#Role selection
role = int(input("Select your role: [Admin = 1,Customer = 2]"))

#Admin login
def adminLogin():
    if(role == 1):
        adminUsername = input("Enter your username: ")
        adminPassword = input("Enter your password: ")
        for line in open("adminLoginDetails.txt","r").readlines():
            login_info = line.split
            if (adminUsername == login_info [0] ) and (adminPassword == login_info[1]):
                print("You have successfully logged in!")
            else:
                print("Invalid username or password,please try again.")

#Customer registration
    else:
        def cusRegistration():  
            registration = input("Are you a registered customer? [Yes/No]")
            if (registration == "No"):
                cusUsername = input("Enter your username: ")
                cusPassword = input("Enter your password: ")
                file = open("customerDetails.txt","a")
                file.write(cusUsername)
                file.write(" ")
                file.write(cusPassword)
                file.write("\n")
                file.close()
                if cusRegistration():
                    print("You have successfully created an account!")
            
#Customer Login
def cusLogin():
    if (registration == "Yes"):
        cusUsername = input("Enter your username: ")
        cusPassword = input("Enter your password: ")
        for line in open("customerDetails.txt","r").readlines():
            loginInfo = line.split()
            if (cusUsername == loginInfo[0]) and (cusPassword == loginInfo[1]):
                print ("You have successfully logged in!")
            else:
                print("Invalid username or password,please try again.")

解决方法

您需要实际运行您的函数:

def do_something():
    print('something')

不会跑。你还需要使用这个:

do_something()

就你而言:

if role == 1:
    adminLogin()

,

在上面给出的代码中,您需要在使用它们的方法之外提取滚动检查。目前,没有什么要求您的方法执行。

role = int(input("Select your role: [Admin = 1,Customer = 2]"))
if role == 1:
     adminLogin()
if role == 2:
     cusLogin()
# etc

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。