[英]I'm new to programming and i'm trying to make a program that lets me login or register
[英]I'm trying a beginner project to make like a login and register program with a txt file “serving as a database” in python
我想要這個:如果用戶輸入.txt
文件中已經存在的email,則output應該是這個email已經存在。
我不需要其他任何幫助,就這件事。 而且我知道這還沒有完成,只是被困在這里......
這是代碼:
choice=input("Choose if you want to register(1) or login(2): ")
if int(choice)==1:
reg=input("Input your email: ")
passreg1=input("Create your password: ")
passreg2=input("Confirm your password: ")
f=open("email.txt","r")
lines=str(f.read())
if reg==any(lines):
print("You already have an account!")
else:
if str(passreg1)==str(passreg2):
#f=open("email.txt", "a")
#f.write("\n" + reg)
#f.close()
print("Account registered successfully.")
else:
print("Passwords do not match.")
else:
log=input("Input your email: ")
passlog=input("Input your password: ")
print("Login successful!")
此外,在文本文件中,我只有這樣排列的電子郵件:
test@gmail.com
你好@yahoo.com
而不是if reg == any(lines):
,要檢查reg
是否在列表lines
中,我們這樣做: if reg in lines:
替換這些行:
f=open("email.txt","r")
lines=str(f.read())
if reg==any(lines):
print("You already have an account!")
通過這些:
with open("email.txt", "r") as f:
lines = f.read()
if reg in lines:
print("You already have an account!")
如果您有一個每行帶有 email 的 txt,您可以將文件內容作為字符串讀取,然后使用字符串splitlines()方法將其轉換為行列表。 之后,您可以檢查列表的任何條目(一行)是否包含您正在尋找的 email:
lines=str(f.read().splitlines())
if reg in lines:
# ...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.