我正在尝试创建一个测验,该测验应该具有将用户输入存储到外部文件中的注册功能。我只需要有关如何存储用户输入的帮助。
这是我的代码:
def register():
name = input("Please enter your full name: ")
age = input("Please enter your age: ")
year = input("Please enter your year group: ")
username = (name[:3] + age)
print("Your username is " + username)
password = input("Please enter a password: ")
答案 0 :(得分:1)
编程可能会造成混乱,您所需要做的只是组织起来。
这是为您准备的算法。
和代码:
#Create a new function
def register():
#Open a file to store the input
with open("credentials.txt", 'a') as out:
#Ask for the data
name = input("Please enter your full name: ")
age = input("Please enter your age: ")
year = input("Please enter your year group: ")
username = (name[:3] + age)
print("Your username is " + username)
#Save to the file
out.write(username + "\n")
password = input("Please enter a password: ")
out.write(password + "\n")
print("Login created!")
#Close the file
out.close()
#Entry point of your program
register()
答案 1 :(得分:0)
您可以像这样将这些变量写入文件:
filehandle = open("filename", "a")
filehandle.write(name + "\t" + age + "\t" + age + "\t" + year +"\t" + password + "\t")
filehandle.close()
答案 2 :(得分:0)
执行此操作的方法有多种,下面我将全部提及: