簡體   English   中英

將信息添加到文本文件,TypeError:'_io.TextIOWrapper'對象不可下標

[英]Adding information to a text file, 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM