python - 向文本文件添加信息,TypeError: '_io.TextIOWrapper' object is not subscriptable
问题描述
基本上我试图让它保存一个测试分数,你得到一个文本文件,其中还包含你的用户名和密码,当你完成测验时会继续添加到这个文件中,但我一直在后面上面写着“你得到了 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
有错误的代码部分是:
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")
我的整个代码是:
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
是您打开的文件句柄usernamelist=open("username.txt","r")
,并且您将其视为带有 的字典/列表listy=usernamelist[line]
,这会导致上述错误。
阅读其余代码,很明显您只想listy
成为与用户名匹配的行,因此如果您将该行替换listy=usernamelist[line]
为listy=line
.
推荐阅读
- javascript - 在 javascript 会话中存储值并在需要时访问它
- yii2 - Yii2如何在索引页添加输入字段
- javascript - 无法使用 Link 控制台记录道具
- angular - 如何检查使用本机元素的 Angular 测试是否导致错误
- angular - 电子邮件值作为 NULL 传递
- python - 执行 Pandas Goupby 和百分比增加/减少
- javascript - 如何跟踪来自不同 UI 的按钮点击?
- influxdb - 如何解释电报高基数?
- google-chrome - 电子网络视图在极少数情况下被打乱
- sql - 从仓库库存中提取数据,按文章分组,但如果描述发生变化,则使用最后一个 USED