python读取/写入文件登录脚本

时间:2018-09-21 07:20:49

标签: python python-requests text-files

我想知道是否有人会帮助。我是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
>>> 

有人对如何仅显示您输入的条目有想法吗?

谢谢

4 个答案:

答案 0 :(得分:2)

就像Sharku说的那样,如果在下面,请将打印内容(用户名,密码)放在您的打印机中。在键入用户名和密码后,还要清楚地写出用户名和密码,这并不是一个聪明的举动,删除它,只在用户登录时让您输入信息即可!

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")

答案 1 :(得分:0)

如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")

答案 2 :(得分:0)

您的“ for循环”将遍历文件中的每个条目,这意味着对于文件中的每个条目,由于以下行,for循环将打印该条目:

    print(username,password)

如果您不希望它打印文件中的所有值,请删除此行代码。 正如其他人所建议的那样,在if语句中添加“ break”将意味着,一旦循环找到与用户输入的条目匹配的条目,它将退出循环,而不会继续不必要地遍历所有值。 / p>

您可以执行以下操作:

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

    else:
        continue

这意味着当用户输入与文件中的条目不匹配时,循环将继续直到找到匹配项为止。 但是,如果用户不存在,则不会考虑您的代码。

答案 3 :(得分:0)

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()
相关问题