簡體   English   中英

python讀/寫文件登錄腳本

[英]python read/write to file login script

我想知道是否有人會幫忙。 我是python的新手。 我正在嘗試為游戲創建一個基本的登錄腳本,它將用戶名和密碼寫入文本文件。 登錄時,它將從該文本文件中讀取並比較用戶所做的輸入。 代碼如下:

def Register():
    print("Hello! You need to register an account before you can begin")
    username = input("Please enter a username: ")
    password = input("Now please enter a password: ")
    file = open("Login.txt","a")
    file.write (username)
    file.write (",")
    file.write (password)
    file.write("\n")
    file.close()
    print ("Your login details have been saved. ")
    print("You will now need to login")
    Login() 

def Login():
    print("Please enter your details to log in")
    username1 = input("Please enter your username: ")
    password1 = input("Please enter your password: ")

    file = open("Login.txt","r")

    for row in file:
        field = row.split(",")
        username = field[0]
        password = field[1]
        lastchar = len(password)-1
        password = password[0:lastchar]
        print(username,password)

        if username1 == username and password1 == password:
            print("Hello",username)

        else:
            print("incorrect")

#file.close()


 user=input("Are you already a user? ")

 if user == "Yes":
     Login()

 elif user =="No":
     Register()

 print("Welcome to our game")

我已經輸入了存儲在文本文件中的第二個用戶,它似乎正在工作,但它檢查了我的第一個條目並說它不正確,然后循環到第二個條目。 這是我不斷得到的輸出:

Are you already a user? Yes
Please enter your details to log in
Please enter your username: jen
Please enter your password: ben
tess bess
incorrect
jen ben
Hello jen
Welcome to the dice game
>>> 

有沒有人知道如何只顯示您輸入的條目?

謝謝

就像 Sharku 說的,把打印(用戶名,密碼)放在你的 if 下面。 在用戶輸入后寫清楚用戶名和密碼也不是一個明智的舉動,刪除它並在用戶登錄時讓您的消息!

for row in file:
    field = row.split(",")
    username = field[0]
    password = field[1]
    lastchar = len(password)-1
    password = password[0:lastchar]

    if username1 == username and password1 == password:
        print("Hello",username)

    else:
        print("incorrect")

正如 sytech 所說,您可以使用 for 循環的breakelse子句:

for row in file:
    field = row.split(",")
    username = field[0]
    password = field[1]
    lastchar = len(password)-1
    password = password[0:lastchar]

    if username1 == username and password1 == password:
        print("Hello",username)
        break
else:
    print("incorrect")

您的“for 循環”將遍歷文件中的每個條目,這意味着對於文件中的每個條目,您的 for 循環會打印由於此行而導致的條目:

    print(username,password)

如果您不希望它打印文件中的所有值,請刪除這行代碼。 正如其他人所建議的那樣,在您的 if 語句中添加一個“中斷”將意味着一旦您的循環找到與用戶輸入的條目匹配的條目,它將離開循環並且不會繼續不必要地遍歷所有值。

你可以這樣做:

    if username1 == username and password1 == password:
        print("Hello",username)
        break

    else:
        continue

這意味着當用戶輸入與文件中的條目不匹配時,循環將繼續直到找到匹配項。 但是,如果用戶不存在,您的代碼不會考慮在內。

import os.path

if not os.path.exists('register.txt'):
    file = open('register.txt', 'w')
    file.close()


def register():
    username = input('Enter username: ')
    if username in open('register.txt', 'r').read():
        print('Username already exists')
        exit()
    password = input('Enter password: ')
    c_password = input('Enter confirm password: ')
    if password != c_password:
        print('Sorry password not match')
        exit()
    handle = open('register.txt', 'a')
    handle.write(username)
    handle.write(' ')
    handle.write(password)
    handle.write('\n')
    handle.close()
    print('User was successfully registered')
    exit()


def login():
    username = input('Enter username: ')
    password = input('Enter password: ')
    get_data = open('register.txt', 'r').readlines()
    users_data = []
    for user in get_data:
        users_data.append(user.split())

    total_user = len(users_data)
    increment = 0
    login_success = 0
    while increment < total_user:
        usernames = users_data[increment][0]
        passwords = users_data[increment][1]
        if username == usernames and password == passwords:
            login_success = 1

        increment += 1

    if login_success == 1:
        print('Welcome ' + username)
    else:
        print('invalid username & password')


question = input('Do you have an account?/yes/no')

if question == 'yes':
    login()
else:
    register()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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