Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 程序不会让我只存储用户的最后三个分数_Python_File_Text_Python Idle - Fatal编程技术网

Python 程序不会让我只存储用户的最后三个分数

Python 程序不会让我只存储用户的最后三个分数,python,file,text,python-idle,Python,File,Text,Python Idle,我试图让程序只保存用户的最后三个分数,而不是将所有分数保存到文本文件中 现在看起来是这样的: if class_number == 0: #This will create and open a new text file under the name #of the class_tag variable. file = open("Class 0" + ".txt", "a") #This will write down the user's name and

我试图让程序只保存用户的最后三个分数,而不是将所有分数保存到文本文件中

现在看起来是这样的:

if class_number == 0:
    #This will create and open a new text file under the name
    #of the class_tag variable.
    file = open("Class 0" + ".txt", "a")
    #This will write down the user's name and their score
    file.write(str(name) + " scored " + str(score))
    #This will create a new line for each user
    file.write("\n")
    #This will close the file.
    file.close()

import collections

def new3ElementDeque():
   return collections.deque([], 3)

nameTop3 = collections.defaultdict(new3ElementDeque)

with open("Class 0.txt") as f:
    for line in f:
        user, score = line.split(':')
        nameTop3[name].append(score)
student scored 3
student scored 8
student scored 0
student scored 4
student scored 10
student scored 3
student scored 0
student scored 4
student scored 3
student scored 0
student scored 4
我希望它是这样的:

if class_number == 0:
    #This will create and open a new text file under the name
    #of the class_tag variable.
    file = open("Class 0" + ".txt", "a")
    #This will write down the user's name and their score
    file.write(str(name) + " scored " + str(score))
    #This will create a new line for each user
    file.write("\n")
    #This will close the file.
    file.close()

import collections

def new3ElementDeque():
   return collections.deque([], 3)

nameTop3 = collections.defaultdict(new3ElementDeque)

with open("Class 0.txt") as f:
    for line in f:
        user, score = line.split(':')
        nameTop3[name].append(score)
student scored 3
student scored 8
student scored 0
student scored 4
student scored 10
student scored 3
student scored 0
student scored 4
student scored 3
student scored 0
student scored 4
但是,空闲shell声明:

if class_number == 0:
    #This will create and open a new text file under the name
    #of the class_tag variable.
    file = open("Class 0" + ".txt", "a")
    #This will write down the user's name and their score
    file.write(str(name) + " scored " + str(score))
    #This will create a new line for each user
    file.write("\n")
    #This will close the file.
    file.close()

import collections

def new3ElementDeque():
   return collections.deque([], 3)

nameTop3 = collections.defaultdict(new3ElementDeque)

with open("Class 0.txt") as f:
    for line in f:
        user, score = line.split(':')
        nameTop3[name].append(score)
student scored 3
student scored 8
student scored 0
student scored 4
student scored 10
student scored 3
student scored 0
student scored 4
student scored 3
student scored 0
student scored 4
如何让程序存储用户的最后三个分数并将其保存到文本文件中

输入的名称为:

    name, score = line.split(':')
ValueError: need more than 1 value to unpack

中没有“:”,因此当它尝试将其按“:”拆分时,结果只有一个元素,并且您无法分配用户和分数,因为只有一个元素

如果文件中的所有行都像“blah scored x”,则可以将行
user,score=line.split(':')
替换为
user,score=line.split('scored')


行中似乎没有“:”,因此当它尝试按“:”进行拆分时,结果只有一个元素,您无法分配
用户
分数
,因为只有一个元素。请在
用户,分数=行之前执行
打印行
。拆分(“:”)
,并检查是否没有“:”Julien Spronck,如何使用“打印行”?像这样:“print line user,score=line.split('scored')?你能显示输入文件吗?我建议你通读python教程(),我更新了我的代码,它开始工作了,但当我再次这样做时,它就不工作了。出现了:name,score=line.split('scored'))ValueError:需要超过1个值才能解压文件中并非所有行都像“blah scored x”。在f:
中的行的行之间键入
打印行
,score=line.split('scored')
,并告诉我们在错误消息出现之前屏幕上打印了什么。在整个程序启动之前,由于'line',它会显示'invalid syntax',因为'line'。我不知道您所说的“因为'line'”是什么意思。看看我刚才在回答中发布的代码。。。我不认为这里面有语法错误。。。是你试过的吗?