Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/312.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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-Can';不要在列表中添加超过1分_Python_List_Append - Fatal编程技术网

Python-Can';不要在列表中添加超过1分

Python-Can';不要在列表中添加超过1分,python,list,append,Python,List,Append,对不起,如果我弄错了,这是我的第一篇文章 我写了一个程序来记录板球运动员的名单和他们的得分,并在他们的记录中添加新的得分,这很好,但是当我添加一个新的击球手时,我不能添加超过一个得分。有人能看出我做错了什么吗?我已经做了两天了。代码如下:- import pickle scores = [["Moe", 100], ["Curly", 50], ["Larry", 0]] #setup the save function same as usual def save_scores():

对不起,如果我弄错了,这是我的第一篇文章

我写了一个程序来记录板球运动员的名单和他们的得分,并在他们的记录中添加新的得分,这很好,但是当我添加一个新的击球手时,我不能添加超过一个得分。有人能看出我做错了什么吗?我已经做了两天了。代码如下:-

import pickle
scores = [["Moe", 100], ["Curly", 50], ["Larry", 0]]

#setup the save function same as usual
def save_scores():
    file = open("pickle.dat", "wb")
    pickle.dump(scores, file)
    file.close()
    print('Scores Saved')
    print()

#setup the load same as yours    
def load_scores():
    file = open("pickle.dat", "rb")
    scores = pickle.load(file)
    file.close()
    print(scores)
    print()

#print the scores to the screen
def print_scores():
    for i in range(0,len(scores)):
        print(scores[i])
    print()

#add a score to the list        
def add_score():
    #first setup a flag like in a bubble sort. This will show if we find a name
    flag = False
    a = ''
    #ask for the name and the new score
    name = input("Enter your name: ")
    score = int(input("Enter your score: "))
    #now count through the list of names
    for i in range (0,len(scores)):
        #take out each name and store it in var a
        a = (scores[i][0])
        #strip the space off the end. Unfortunately, when Python stores names
        #in a list it adds spaces to the end of them. The only way to strip them
        #off is to put them in a variable first, hence a
        a.replace(' ','')
        #now we set the flag if we find the name we just typed in
        if a == name:
            #and add the score to the name in the list
            scores[i].append(score)
            flag = True
    #if we get to here and the flag is still false then the name doesn't exist
    #in the list so we add the name and the score to the list
    if flag == False:
        arr = (name,score)
        scores.append(arr)

def print_menu():
    print('1. Print Scores')
    print('2. Add a Score')
    print('3. Load Scores')
    print('4. Save Scores')
    print('5. Quit')
    print()

menu_choice = 0
print_menu()
while True:
    menu_choice = int(input("Type in a number (1-5): "))
    if menu_choice == 1:
        print_scores()
    if menu_choice == 2:
        add_score()
    if menu_choice == 3:
        print_scores()
    if menu_choice == 4:
        save_scores()
    if menu_choice == 5:
        print ("Goodbye")
        break
替换:

arr = (name,score)
与:

arr = [name,score]

问题是,当添加一个新播放器时,您正在使用一个
元组。但是
元组
不可变的在python中,您不能修改它们。这就是你无法向新玩家追加更多分数的原因。将
元组
替换为
列表
将解决此问题,因为
列表
可变的

您所说的“我不能添加多个分数”的确切含义是什么?请添加一些示例输入并显示其输出。您的问题非常令人困惑抱歉,如果我选择选项2并添加名称Bert并给他一个分数,它会将其保存到列表中,但如果我尝试为Bert添加另一个分数,它将不起作用键入数字(1-5):2输入您的姓名:Bert输入您的分数:50回溯(最近一次呼叫):文件“C:\Users\Mr Atkinson\Documents\Python\test.py”,第69行,添加分数()文件“C:\Users\Mr Atkinson\Documents\Python\test.py”,第45行,添加分数[i]。追加(分数)AttributeError:“tuple”对象没有属性“append”@MikeAtkinson这是因为你使用的是tuple而不是listMan这太棒了,真的谢谢你,我已经试了好几个小时了明白了,你真的很快很出色,再次感谢你