python';非类型';对象是不可编辑的

python';非类型';对象是不可编辑的,python,Python,该程序是高尔夫球场的用户界面。球员和联赛级别不应该改变。用户界面允许用户在文本文件中为每个球员输入9洞分数,并返回每个球员的总分数、PAR和小鸟。在输入最后一组分数后,我得到了这个错误(我不知道如何修复),因此它似乎在验证输入的分数,但在最后也验证了它们,并且它们不再存在。我需要这样做,以便在它们被输入后进行检查,就这样。我不知道该怎么做 Traceback (most recent call last): File "<pyshell#41>", line 1, in <

该程序是高尔夫球场的用户界面。球员和联赛级别不应该改变。用户界面允许用户在文本文件中为每个球员输入9洞分数,并返回每个球员的总分数、PAR和小鸟。在输入最后一组分数后,我得到了这个错误(我不知道如何修复),因此它似乎在验证输入的分数,但在最后也验证了它们,并且它们不再存在。我需要这样做,以便在它们被输入后进行检查,就这样。我不知道该怎么做

Traceback (most recent call last):
  File "<pyshell#41>", line 1, in <module>
    main()
  File "/Users/tinydancer9454/Documents/python/golfRound.py", line 102, in main
    isValidScore(holeScores)
  File "/Users/tinydancer9454/Documents/python/golfRound.py", line 120, in isValidScore
    for score in holeScores:
TypeError: 'NoneType' object is not iterable
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
main()
文件“/Users/tinydancer9454/Documents/python/golfRound.py”,第102行,在main中
isValidScore(holeScores)
isValidScore中的文件“/Users/tinydancer9454/Documents/python/golfRound.py”,第120行
对于holeScores中的分数:
TypeError:“非类型”对象不可编辑
代码如下:

class Player:
    """ Represents a player in the golf league """

    PAR = [4, 3, 4, 3, 4, 5, 4, 3, 5]
    """ par for each of the 9 holes """

    def __init__(self, name):
        """ creates a Player and keeps track of stats """
        self.__name = name
        self.__pars = 0
        self.__birdies = 0
        self.__gross = 0

    def getName(self):
        """ returns a player's name """
        return self.__name

    def getGross(self):
        """ returns a player's gross score """
        return self.__gross

    def getPars(self):
        """ returns number of pars made """
        return self.__pars

    def getBirdies(self):
        """ returns number of birdies made """
        return self.__birdies

    def recordScores(self, holeScores):
        """ mutator method that uses the results of one round of play
            (9 holes) to update a player's stats """
        self.__gross = sum(holeScores)
        self.__findparsandbirdies(holeScores)

    def __findparsandbirdies(self, scores):
        """ helper method that finds the number of pars and birdies """

        pars = 0
        birdies = 0
        hole = 0
        for score in scores:
            if score == Player.PAR[hole]:
                pars += 1
            if score == Player.PAR[hole] - 1:
                birdies += 1
            hole += 1
        self.__pars = pars
        self.__birdies = birdies

    def __str__(self):
        """ returns a string representation of a player """
        return 'a Player named ' + self.__name

class League:
    """ represents the players of a golf league """

    def __init__(self, fileName = 'players.txt'):
        """ creates a list of Player objects from the
            names stored in the file specified """
        self.__playerList = []
        datafile = open(fileName, 'r')
        for line in datafile:
            playerName = line.rstrip()
            player = Player(playerName)
            self.__playerList.append(player)

    def getNumPlayers(self):
        """ returns the number of players is the league """
        return len(self.__playerList)

    def getPlayerbyPosition(self, position):
        """ returns the player at the specified position """
        return self.__playerList[position]

    def getPlayerbyName(self, name):
        """ returns the player with the specified name """
        for player in self.__playerList:
            if player.getName() == name:
                return player
        return None

    def __str__(self):
        return 'a golf league with ' + str(self.getNumPlayers()) + ' players'



def main():
    """The input and output for the program"""
    l= League()
    players= []
    holeScores= enterScores(l, players)
    isValidScore(holeScores)
    output(players)


def enterScores(l, players):
    """enter the scores"""
    for position in range(l.getNumPlayers()):
        inputScore= input("Please enter a list of the player's scores: ")
        holeScores= (int(score) for score in inputScore.split(','))
        for score in holeScores:
            while not isValidScore(holeScores):
                holeScores= input("Please enter a valid list of scores: ")
        p= l.getPlayerbyPosition(position)
        players.append(p.recordScores(holeScores))
        p.recordScores(holeScores)

def isValidScore(holeScores):
    """checks if the scores entered for a single hole are 1 to 10, inclusive"""
    for score in holeScores:
        if not int:
            return False
        elif score < 1:
            return False
        elif score > 10:
            return False
    return True


def output(grossScore, pars, birdies):
    """prints output"""
    for player in players:
        print(player.getName())
        print('Gross score:', player.getGross())
        print('Pars: ', player.getPars())
        print('Birdies: ', player.getBirdies())
职业玩家:
“”“代表高尔夫联盟中的球员”“”
PAR=〔4, 3, 4,3, 4, 5,4, 3, 5〕
“9孔中的每一个”
定义初始化(self,name):
“”“创建玩家并跟踪统计信息”“”
self.\uu name=名称
自身参数=0
赛尔夫·小鸟=0
总自我价值=0
def getName(self):
“”“返回玩家的名字”“”
返回self.\u名称
def getGross(自我):
“”“返回玩家的总分”“”
返回自我。总的
def GETPAR(自我):
“”“返回生成的PAR数”“”
返回自我
def getBirdies(自我):
“”“返回生成的小鸟数”“”
回归自我
def记录分数(自我、holeScores):
“”“使用一轮游戏结果的mutator方法”
(9洞)更新玩家的统计信息“”
总金额=总金额(holeScores)
自寻雀鸟(holeScores)
def__findparsandbirdies(自我,得分):
“”“查找PAR和小鸟数量的助手方法”“”
pars=0
小鸟=0
孔=0
对于分数中的分数:
如果得分==球员标准杆[洞]:
pars+=1
如果得分==球员标准杆[洞]-1:
小鸟+=1
孔+=1
self.\u pars=pars
赛尔夫·小鸟=小鸟
定义(自我):
“”“返回播放器的字符串表示形式”“”
返回'a Player named'+self.\u name
班级联盟:
“”“代表高尔夫联盟的球员”“”
def u uu init uu uu(self,文件名='players.txt'):
“”“从中创建播放器对象的列表。”
存储在指定文件“”中的名称
self.\uu playerList=[]
数据文件=打开(文件名“r”)
对于数据文件中的行:
playerName=line.rstrip()
player=player(playerName)
self.\uu playerList.append(播放器)
def getNumPlayers(自):
“”“返回联盟中的球员人数”“”
返回len(self.\u playerList)
def getPlayerbyPosition(自身,位置):
“”“返回指定位置的播放机”“”
返回自我。玩家列表[位置]
def getPlayerbyName(自身,名称):
“”“返回具有指定名称的播放机”“”
适用于自我中的玩家。\玩家列表:
如果player.getName()==名称:
还击球员
一无所获
定义(自我):
返回'a golf league with'+str(self.getNumPlayers())+'players'
def main():
“”“程序的输入和输出”“”
l=联盟()
玩家=[]
holeScores=输入分数(l,球员)
isValidScore(holeScores)
输出(玩家)
分数(l,球员):
“输入分数”
对于范围内的位置(l.getNumPlayers()):
inputScore=input(“请输入玩家的分数列表:”)
holeScores=(int(score)表示inputScore.split(',')中的分数
对于holeScores中的分数:
虽然不是isValidScore(holeScores):
holeScores=输入(“请输入有效的分数列表:”)
p=l.getPlayerbyPosition(位置)
玩家。附加(p.记录分数(holeScores))
p、 记录分数(holeScores)
def isValidScore(holeScores):
“”“检查为单个孔输入的分数是否为1到10,包括1到10”
对于holeScores中的分数:
如果不是int:
返回错误
elif评分<1分:
返回错误
elif评分>10分:
返回错误
返回真值
def输出(GrossCore、pars、birdies):
“”“打印输出”“”
对于玩家中的玩家:
打印(player.getName())
打印('Gross score:',player.getGross())
打印('Pars:',player.getPars())
打印('Birdies:',player.getBirdies())

main
中,您将
enterScore
的返回值分配给
holeScores
,它是
None
,因为
enterScore
没有
return
语句。因此,
None
被传递给
isValidScore
,它尝试在
for
循环中对它进行迭代,但失败了,因为
None
不是可以循环的东西。

那么返回语句应该是什么呢?返回holeScores?是否应将其与holeScores或其他地方的得分的
对齐?此时,
holeScores
将丢失,因为您没有在任何地方跟踪它们。顺便说一句,您已经在输入之后验证了它们,那么为什么要在流程结束时再次验证它们呢?我试图使它们只在每次输入之后才被验证,我没有意识到它在最后再次验证了它们。我明白你的意思,我怎样才能跟踪每套分数?如果它在输入时检查每个holeScore,则holeScore在每次输入后都会更改,但