2

我的代码有点卡住了我正在尝试创建一个登录系统,用户可以登录到他们的帐户并使用我设置的命令,但我想添加一些额外的输入,以便用户可以注册到登录系统并使用我设置的命令。我想每次都将用户的输入永久存储在不同的变量中,这样当用户重新启动代码时,他们就可以登录系统而无需再次注册。

这是我到目前为止创建的一段代码:

print ("Welcome!")
print ("Would you like to register")
loop = True

while (loop == True):
    username = input ("username: ")
    password = input ("password: ")
    print ("register here if you don't have an account")
    username1 = input ("name: ")
    print ("this is what you use to login to the system")
    username2 = input ("username: ")
    username3 = input ("password: ")

    if (username == "rohit" and password == "rodude") :
        print ("hello and welcome " + username or )
        loop = False
        loop1 = True

    else:
        print ("invalid username and password")

    while(loop1 == True):

        command = str(input(username + "{} > >"))
        if(command.lower() == "exit"):
            loop1=False

        elif(command.lower() == "hi"):
            print("Hi " + username + "!")

        else:
            print ("'" + command + "' is an invalid command!")
4

2 回答 2

1

嘿,伙计们,你们的方式太复杂了,你们能做的就是这个

    name = open("usernames.txt", "w") #opens file usernames.txt and gets ready to write to it
file = input("please type some text: ") #asks user for text in code

name.write(file) #writes contents in file to usernames.txt
name.close() #closes file
open1 = open("usernames.txt", "r") #opens file to read it
print (open1.read()) #prints whatever is in the text file
于 2015-10-20T19:13:55.890 回答
0

您不能将变量用于本地存储。如果您希望信息在程序运行期间持久存在,则需要将其存储在持久位置 - 通常是磁盘文件或数据库。有很多模块可以让这更容易,Pickle(如 klashxx 的回复中所述)对于简单场景来说是一个很好的模块。

于 2014-07-02T18:38:19.540 回答