import sys
import time
import os
def typer(x):
for i in x:
print(i, end='')
time.sleep(0.040)
def register():
name = input('Create username: ')+'.txt'
file = open(name, 'a')
i = input("Create password: ")
file.write(i+'\n')
typer("Creating account...")
def login():
user = input("Insert username: ")
try:
file = open(user, "r")
except FileNotFoundError:
print("Username\Password incorrect")
sys.exit()
pas = input("Enter password: ")
try:
file = open(user, 'r')
if user == file:
pasmatch = file.read(1)
if pas == pasmatch:
typer("Logging into your account...")
except:
print("Username\Passowrd i incorrect")
sys.exit()
menu = True
while menu == True:
menu = True
print("What do you want to do?")
print("1. Register")
print("2. Login")
ii = input("")
if ii == "1":
register()
if ii == "2":
login()
menu = False
帮助,我创建了这个程序,因此它会生成一个包含您帐户详细信息的保存文件。但登录功能不起作用,我不知道什么是错的。即使用户名匹配,它仍然在incorect中说明
答案 0 :(得分:-2)
我认为这个块;
file = open(user, 'r')
if user == file:
pasmatch = file.read(1)
if pas == pasmatch:
typer("Logging into your account...")
你的意思是:
file = open(user, 'r')
if pas == file.read(): #or maybe even if pas in file.read():
typer("Logging into your account...")
没有详细说明,您的初始代码在等同事物方面存在一些问题(user == file
没有任何意义,因为user
是一个字符串而file
是一个字符串fileObject)或你如何阅读(read(1)
只读取一个字节)。
答案 1 :(得分:-2)
import sys
import time
import os
def typer(x):
for i in x:
print(i, end='')
time.sleep(0.020)
def register():
name = input('Create username: ')+'.txt'
file = open(name, 'a')
i = input("Create password: ")
file.write(i+'\n')
typer("Creating account...")
def login():
user = input("Insert username: ")
try:
file = open((user+'.txt'), "r") # line altered
except FileNotFoundError:
print("Username\Password incorrect")
sys.exit()
pas = input("Enter password: ")
pas += '\n' # line altered
try:
file = open((user+'.txt'), 'r')
if (user+'.txt') == file.name: # line altered
pasmatch = file.read() # line altered, removed '1'
if pas == pasmatch:
typer("Logging into your account...")
except:
print("Username\Password i incorrect")
sys.exit()
menu = True
while menu == True:
menu = True
print("What do you want to do?")
print("1. Register")
print("2. Login")
ii = input("")
if ii == "1":
register()
if ii == "2":
login()
menu = False
这应该这样做。我看到了使用文件对象作为文件名的问题。 如果有帮助,请务必将此标记为解决方案。谢谢!