Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python读/写文件登录脚本_Python_Python Requests_Text Files - Fatal编程技术网

python读/写文件登录脚本

python读/写文件登录脚本,python,python-requests,text-files,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

我想知道是否有人能帮忙。我是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
>>> 
有人知道如何只显示您输入的条目吗


谢谢

就像夏库说的那样,在下面的if中输入打印内容(用户名、密码)。另外,在用户输入用户名和密码后清楚地写下用户名和密码也不是一个真正的智能moove,删除它,在用户登录时只显示您的消息

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

正如夏库所说,在下面的if中输入打印内容(用户名、密码)。同时,在用户键入用户名和密码后清楚地写下用户名和密码。这并不是一个真正的智能moove,请删除它,并在用户登录时让您的消息显示出来

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循环的
break
else
子句:

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

正如sytech所说,您可以使用for循环的
break
else
子句:

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语句中添加一个“break”将意味着一旦循环找到与用户输入的条目相匹配的条目,它将离开循环,不再继续不必要地遍历所有值

你可以这样做:

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

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

您的“for循环”将在文件中的每个条目中循环,这意味着对于文件中的每个条目,for循环将打印该条目,因为这一行:

    print(username,password)
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()
如果不希望打印文件中的所有值,请删除这行代码。 按照其他人的建议,在if语句中添加一个“break”将意味着一旦循环找到与用户输入的条目相匹配的条目,它将离开循环,不再继续不必要地遍历所有值

你可以这样做:

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

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

import os.path
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()
如果不存在os.path.exists('register.txt'): 文件=打开('register.txt','w') file.close()文件 def寄存器(): 用户名=输入('输入用户名:') 如果用户名处于打开状态('register.txt','r').read(): 打印('用户名已存在') 退出() 密码=输入('输入密码:') c_password=input('输入确认密码:') 如果输入密码!=c_密码: 打印('对不起,密码不匹配') 退出() handle=open('register.txt','a') handle.write(用户名) handle.write(“”) handle.write(密码) handle.write(“\n”) handle.close() 打印('用户已成功注册') 退出() def login(): 用户名=输入('输入用户名:') 密码=输入('输入密码:') get_data=open('register.txt','r')。readlines() 用户_数据=[] 对于get_数据中的用户: users\u data.append(user.split()) 总计用户=len(用户数据) 增量=0 登录成功=0 当增量<总用户时: 用户名=用户\数据[增量][0] 密码=用户\数据[增量][1] 如果用户名==用户名,密码==密码: 登录成功=1 增量+=1 如果登录成功==1: 打印('Welcome'+用户名) 其他: 打印('无效的用户名和密码') 问题=输入('您有账户吗?/是/否') 如果问题=‘是’: 登录() 其他: 寄存器()
导入操作系统路径
如果不存在os.path.exists('register.txt'):
文件=打开('register.txt','w')
file.close()文件
def寄存器():
用户名=输入('输入用户名:')
如果用户名处于打开状态('register.txt','r').read():
打印('用户名已存在')
退出()
密码=输入('输入密码:')
c_password=input('输入确认密码:')
如果输入密码!=c_密码:
打印('对不起,密码不匹配')
退出()
handle=open('register.txt','a')
handle.write(用户名)
handle.write(“”)
handle.write(密码)
handle.write(“\n”)
handle.close()
打印('用户已成功注册')
退出()
def login():
用户名=输入('输入用户名:')
密码=输入('输入密码:')
get_data=open('register.txt','r')。readlines()
用户_数据=[]
对于get_数据中的用户:
users\u data.append(user.split())
总计用户=len(用户数据)
增量=0
登录成功=0
当增量<总用户时:
用户名=用户\数据[增量][0]
密码=用户\数据[增量][1]
如果用户名==用户名,密码==密码:
登录成功=1
增量+=1
如果登录成功==1:
打印('Welcome'+用户名)
其他:
打印('无效的用户名和密码')
问题=输入('您有账户吗?/是/否')
如果问题=‘是’:
登录()
其他:
寄存器()

删除
打印(用户名、密码)
行?或者放入if子句问题是流量控制。提示:
for
循环可以使用
else
子句,如果整个循环完成而没有遇到
break
(或
return
)语句,则将执行该子句。也许要