将信息添加到文本文件,类型错误:'_io.TextIOWrapper'对象不是subscriptab

2024-04-23 06:47:00 发布

您现在位置:Python中文网/ 问答频道 /正文

基本上,我试图让它保存一个测试分数,你也得到了一个文本文件,也包含了你的用户名和密码,并将继续添加到这个文件,当你完成测试,但我继续在这一行,它说明了类似“你得到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

出现错误的代码部分是:

^{pr2}$

我的全部代码是:

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()

Tags: importfalsetrueifeasyusernamemediumgrade
1条回答
网友
1楼 · 发布于 2024-04-23 06:47:00

usernamelist是用usernamelist=open("username.txt","r")打开的文件句柄,您将其视为带有listy=usernamelist[line]的dict/list,这会导致上述错误。在

在阅读其余代码时,很明显您希望listy仅仅是与用户名匹配的行,因此如果您将listy=usernamelist[line]替换为listy=line,那么就可以了。在

相关问题 更多 >