Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/305.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 - Fatal编程技术网

Python 我做了一个登录系统,但当我运行它时,它会产生一个意想不到的结果。你能修复它或改进它吗?

Python 我做了一个登录系统,但当我运行它时,它会产生一个意想不到的结果。你能修复它或改进它吗?,python,Python,我后悔地在python上开始了面向对象的编程,所以我创建了一个登录系统,但当我运行它时,它最终会产生一个意想不到的结果 它本应打印“登录成功”,但没有打印,并出现“登录失败”,即使它打印模块和文件中的值,并且它们看起来相同,但它输出的结果不是? 有人知道为什么会这样吗 firstname="food"#given veriables lastname="boyyyyyy"#given veriables username=firstname+lastname#given veriables

我后悔地在python上开始了面向对象的编程,所以我创建了一个登录系统,但当我运行它时,它最终会产生一个意想不到的结果

它本应打印“登录成功”,但没有打印,并出现“登录失败”,即使它打印模块和文件中的值,并且它们看起来相同,但它输出的结果不是? 有人知道为什么会这样吗

firstname="food"#given veriables 
lastname="boyyyyyy"#given veriables 
username=firstname+lastname#given veriables 
password="errrorrr"#given veriables 
thislist = ["rusty_sword", "cloth_shirt", "wooden_shield","health_potion"]#given veriables 
demon_kill=0#given veriables 
potion_number=5#given veriables
gold=10#given veriables
area=0#given veriables


thisdict = {"username": firstname+lastname,"password": password,}#creates dictionary
print(thisdict)#shows dictionary
class saving:#creates class called saving
  def __init__(self,info,equipment_w,equipment_a,equipment_s,equipment_p,demon_kill_obj,potion_number_obj,gold_obj,area_obj):#creates objetcs
    self.thisdict=thisdict#sets objects values
    self.equipment_w=equipment_w#sets objects values
    self.equipment_a=equipment_a#sets objects values
    self.equipment_s=equipment_s#sets objects values
    self.equipment_p=equipment_p#sets objects values
    self.demon_kill_obj=str(demon_kill_obj)#sets objects values
    self.potion_number_obj=str(potion_number_obj)#sets objects values
    self.gold_obj=str(gold_obj)#sets objects values
    self.area_obj=str(area_obj)#sets objects values
  def fileing(self):#creates objetcs
    f=open(self.thisdict["username"]+" personalgame save.txt","a")#opens files from dictionary
    f.truncate(0)#sets file as empty
    f.write(self.thisdict["username"]+"\n")#writes in file using objects
    f.write(self.thisdict["password"]+"\n")#writes in file using objects
    f.write(self.equipment_w+"\n")#writes in file using objects
    f.write(self.equipment_a+"\n")#writes in file using objects
    f.write(self.equipment_s+"\n")#writes in file using objects
    f.write(self.equipment_p+"\n")#writes in file using objects
    f.write(self.demon_kill_obj+"\n")#writes in file using objects
    f.write(self.potion_number_obj+"\n")#writes in file using objects
    f.write(self.gold_obj+"\n")#writes in file using objects
    f.write(self.area_obj)#writes in file using objects
    f.close#closes file
kkkk=saving(thisdict,thislist[0],thislist[1],thislist[2],thislist[3],demon_kill,potion_number,gold,area)#takes info from class and saves it to kkkk
kkkk.fileing()

f=open(thisdict["username"]+" personalgame save.txt","r")#opens the file
lines=f.readlines()#sets points in the file to seperate veriables
print(username)#shows you  what username is in veriables
print(lines[0])#show you what username is in the file
if str(username)==str(lines[0]):#compares the the two veriables
  print("login sucessfull")
else:
  print("login falied")

您尝试比较的两个变量看起来相同,但对于
行[0]
而言,末尾有新行字符
\n
,因此它们并不完全相等:

username == foodboyyyyyy
lines[0] == foodboyyyyyy\n
要解决此问题,可以从变量
行[0]
中删除
\n
字符:

if str(username)==str(lines[0].replace("\n", "")):
    print("login sucessfull")
else:
    print("login falied")

谢谢你的帮助这是我的荣幸