python - 在程序中注册帐户
问题描述
我想为用户创建帐户,因此当他们输入他的个人代码时,程序会回复他的姓名和信息并继续使用该应用程序,到目前为止我有这个但我不知道如何编码。
print("Welcome to xxApp")
YourCode = input("Create your Code: ")
if Yourcode in Users.txt
#Continue with the program
else
Name = input("Please enter your name: ")
Age = input("Enter your age: ")
print(",Name,,YourCode,,Age,"your info has been saved")
#and continue with the program
解决方案
我不确定使用文本文件是否是最好的主意 - CSV 可能会更好。但是,这适用于使用文本文件。
主要.py:
def accounts():
yourCode = input("Please enter your user code. \nIf you already have an account, type your account user code. \nOtherwise, enter a new one to create an account: ") # User types their code
# Opens file of users in read mode
with open("users.txt", "r") as rf:
users = rf.readlines() # Creates a list of users in the .txt file to check
# Checks each user in the file to see if they are already in the accounts list. Each user is split into their user_code, name and age
for each_user in [user.split(",") for user in users]:
if each_user[0] == yourCode: # If the user is in the accounts list
print(f"Welcome {each_user[1]}") # f{} string allows the name to be inserted easily into the print statement
xxApp() # Program runs
return None # Exit Statement to get out of the accounts function when the main program has finished
# If the user reaches this stage, it means they are not in the account list
with open("users.txt", "a") as af: # Open file in append
name = input("Please enter your name: ")
age = input("Enter your age: ")
af.write(f"{yourCode},{name},{age}\n") # Adds the users info to the users.txt file
print(f"Thank you {name}, your information has been added.")
xxApp()
def xxApp():
... # Put your main program here
accounts()
NewUser1,Example,27
推荐阅读
- reactjs - Why I get typescript error: Argument of type 'string | FilterData[] | number[] | Moment[]' is not assignable to parameter of type 'string'?
- materialize - 如何解决显示数据大小不合适的图像:image/gif;base64
- firebase - Flutter:如何将元素添加到 Firebase 数据库中的映射字段?
- html - 有没有办法在 HTML 中多次使用相同的代码(代码可重用性)?
- python - Python:如何在浮点数中拆分字典值
- c++ - 在 C++ 中用 floor、ceil 和向外舍入模式除整数
- python - 是否可以让 Python IDE 为动态生成的类属性提供自动完成功能?
- google-chrome - 有没有可靠的开源可以代理 QUIC 等 UDP 流量?
- java - 不能自动配置会话存储库(会话存储类型为“redis”)
- swift - SKScene 中的对象不调用 Keydown