2013-04-04 142 views
0

該程序是一個高爾夫回合的用戶界面。球員和聯賽的課程不應該改變。用戶界面允許用戶在文本文件中爲每個玩家輸入9個洞分數,併爲每個玩家返回一個總分數,分數和小鳥。在輸入最後一組分數後,我得到了這個錯誤(我不知道該如何解決),因此它似乎在輸入分數時驗證分數,但最後也驗證了分數,它們是不再存在。我需要使它們在輸入後進行檢查,就是這樣。我不知道如何做到這一點python'NoneType'對象不可迭代

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 

下面的代碼:

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

回答

1

main你的enterScore的返回值賦給holeScores,這是None因爲enterScore沒有return聲明。所以None傳遞給isValidScore試圖在其上迭代的for循環,而失敗,因爲None是不是你可以上環。

+0

那麼返回語句應該是什麼?迴歸洞分數?它應該與「洞穴分數」還是其他地方一致? – tinydancer9454 2013-04-04 03:03:37

+0

那時'holescores'丟失了,因爲你沒有在任何地方跟蹤它們。順便說一句,你已經在輸入後驗證它們,那麼爲什麼在過程結束時再次驗證它們呢? – benselme 2013-04-04 03:09:30

+0

我試圖讓它們在每次輸入後才被驗證,我沒有意識到它在最後再次驗證它們。我明白你在說什麼,我將如何跟蹤每組分數? – tinydancer9454 2013-04-04 03:13:22