0

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

这是我的代码:

#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.")
4

2 回答 2

1

您需要实际运行您的功能:

def do_something():
    print('something')

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

do_something()

在你的情况下:

if role == 1:
    adminLogin()

ETC

于 2021-04-23T16:31:33.093 回答
0

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

role = int(input("Select your role: [Admin = 1, Customer = 2]"))
if role == 1:
     adminLogin()
if role == 2:
     cusLogin()
# etc
于 2021-04-23T16:05:18.147 回答