基本上,我正在尝试使其保存测试成绩,即您也获得了一个文本文件,其中也包含您的用户名和密码,并且在完成测验时将继续添加到该文件中,但是我一直在其后的行显示类似“您有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()
答案 0 :(得分:0)
usernamelist
是您使用usernamelist=open("username.txt","r")
打开的文件句柄,您正在使用listy=usernamelist[line]
将其视为字典/列表,这会导致上述错误。
阅读其余的代码,很明显,您希望listy
仅是与用户名匹配的行,因此,如果将行listy=usernamelist[line]
替换为listy=line
,它将起作用。