簡體   English   中英

我正在嘗試一個初學者項目,以在 python 中使用“用作數據庫”的 txt 文件制作登錄和注冊程序

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM