[英]Adding information to a text file, TypeError: '_io.TextIOWrapper' object is not subscriptable
Basically I'm trying to make it so that it saves a test score that you got too a text file that also contains your username and password and will continue to add to this file when you complete quizes, but i keep getting at the line after it states something like "You got 40% and got a D" 基本上,我正在努力做到这一点,以便它保存一个测试成绩,即您也获得了一个文本文件,其中还包含您的用户名和密码,并且在完成测验时将继续添加到该文件中,但之后我一直保持联系上面写着“你有40%的人有D”
Traceback (most recent call last):
File "N:\Y10 Python\course work onbe.py", line 141, in <module>
listy=usernamelist[line]
TypeError: '_io.TextIOWrapper' object is not subscriptable
The part of the code with the error is: 带有错误的代码部分是:
found=False
while found==False:
usernamelist=open("username.txt","r")
for line in usernamelist.readlines():
if (username in line):
found=True
listy=usernamelist[line]
usernamelist.close()
if found==True:
usernamelist = open("username.txt","a")
listy=listy.rstrip(["\n"])
usernamelist.write(","+topic+"-"+difficulty+"-"+grade+"\n")
usernamelist.close()
else:
print ("Invalid username")
My whole code is: 我的整个代码是:
import os
import sys
#Login-in function
nuser=input("Do you already have an account?(Y/N)")
nuser=nuser.lower()
found=False
if nuser==("y"):
while found==False:
username=input("Enter username:")
password=input("Enter password:")
usernamelist=open("username.txt","r")
for line in usernamelist:
if (username+","+password in line):
found=True
if found==True:
print ("Welcome "+username)
else:
print ("Invalid username or password, try again")
usernamelist.close()
#Creating an account
if nuser==("n"):
name=input("Enter name:")
name=name[:3]
age=input("Enter age:")
username=name+age
print ("Your username is "+username)
password=input("Enter a password:")
usernamelist = open("username.txt","a")
usernamelist.write(username+","+password+"\n")
print ("Your account has been created, welcome "+username)
usernamelist.close()
#Option selection
optionchosen=False
option=input("What do you want to do? Take a quiz(a), see a report on all your quizzes(b)or a report on a specific topic(c)?")
option=option.lower()
#Topics and questions
if option==("a"):
optionchosen=True
topicchosen=False
difficultychosen=False
while topicchosen==False:
topicchosen=True
while difficultychosen==False:
difficultychosen=True
topic=input("What topic do you want to be quizzed on?History, Music or Computing")
topic=topic.lower()
difficulty = input("What difficulty do you want, easy, medium or hard?")
difficulty=difficulty.lower()
if topic == ("history"):
topicchoseen=True
if difficulty == ("easy"):
difficultychosen=True
scriptpath = "history_easy.py"
import history_easy
from history_easy import score
elif difficulty == ("medium"):
difficultychosen=True
scriptpath = "history_medium.py"
import history_medium
from history_medium import score
elif difficulty == ("hard"):
difficultychosen=True
scriptpath = "history_hard.py"
import history_hard
from history_hard import score
else:
print ("Invalid difficulty")
difficultychosen=False
elif topic == ("music"):
topicchoseen=True
if difficulty == ("easy"):
difficultychosen=True
scriptpath = "music_easy.py"
import music_easy
from music_easy import score
elif difficulty == ("medium"):
difficultychosen=True
scriptpath = "music_medium.py"
import music_medium
from music_medium import score
elif difficulty == ("hard"):
difficultychosen=True
scriptpath = "music_hard.py"
import music_hard
from music_hard import score
else:
print ("Invalid difficulty")
difficultychosen=False
elif topic == ("computing"):
topicchoseen=True
if difficulty == ("easy"):
difficultychosen=True
scriptpath = "computing_easy.py"
import computing_easy
from computing_easy import score
elif difficulty == ("medium"):
difficultychosen=True
scriptpath = "computing_medium.py"
import computing_medium
from computing_medium import score
elif difficulty == ("hard"):
difficultychosen=True
scriptpath = "computing_hard.py"
import computing_hard
from computing_hard import score
else:
print ("Invalid difficulty")
difficultychosen=False
else:
print("Invalid topic")
topicchoseen=False
print (score)
if score==(5):
grade=("A")
print ("You got 100% and got a "+grade)
elif score==(4):
grade=("B")
print ("You got 80% and got a "+grade)
elif score==(3):
grade=("C")
print ("You got 60% and got a "+grade)
elif score==(2):
grade=("D")
print ("You got 40% and got a "+grade)
elif score==(1):
grade=("E")
print ("You got 20% and got a "+grade)
else:
grade=("F")
print ("You got 0% so got a "+grade)
found=False
while found==False:
usernamelist=open("username.txt","r")
for line in usernamelist.readlines():
if (username in line):
found=True
listy=usernamelist[line]
usernamelist.close()
if found==True:
usernamelist = open("username.txt","a")
listy=listy.rstrip(["\n"])
usernamelist.write(","+topic+"-"+difficulty+"-"+grade+"\n")
usernamelist.close()
else:
print ("Invalid username")
#Report 1
if option==("b"):
optionchosen=True
person=input("Whats the username of the person you want to produce a report for?")
found=False
while found==False:
usernamelist=open("username.txt","r")
for line in usernamelist:
if (person in line):
found=True
print (line)
if found==True:
break
else:
print ("Invalid username or password, try again")
usernamelist.close()
usernamelist
is a file handle that you opened with usernamelist=open("username.txt","r")
, and you are treating it as a dict/list with listy=usernamelist[line]
, which causes the said error. usernamelist
是您使用usernamelist=open("username.txt","r")
的文件句柄,您将其视为具有listy=usernamelist[line]
的字典/列表,这会导致上述错误。
Reading the rest of your code it's apparent that you want listy
to simply be the line that matches the username, so it will work if you replace the line listy=usernamelist[line]
with listy=line
. 读你的代码的其余部分,你希望它是明显listy
简单地是用户名相匹配的线,因此,如果您更换线路,将工作listy=usernamelist[line]
与listy=line
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.