我对我的代码有点困惑,我正在尝试创建一个登录系统,用户可以登录到他们的帐户并使用我设置的命令,但我想添加一些额外的输入,以便用户可以注册登录系统并使用我设置的命令。我想将用户每次输入的内容永久存储在不同的变量中,以便当用户重新启动这段代码时,他们可以登录系统,而无需再次注册。
这是我迄今为止创建的代码片段:
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!")
嘿伙计们,你们的方法太复杂了,你们能做的就是这个
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
您不能使用变量进行本地存储。如果您希望信息在程序运行期间保持不变,则需要将其存储在持久位置 - 通常是磁盘文件或数据库。有很多模块可以使这一过程变得更容易,Pickle(如 klashxx 的响应中所述)对于简单场景来说是一个很好的模块。
隐藏 qwkd wid qw dqwk dqkw dqwk dkwqj dkqwd qwdk dqw