2016-11-27 95 views
1

我想弄清楚它爲什麼會拋出這個錯誤。元組的長度應該是4,並且是。任何提示,想法?這是爲什麼拋出一個IndexError?

此代碼適用於我正在爲簡化編程Udacity納米程度工作的瑞士風格項目。從tournament.py

相關代碼Python代碼:

def testStandingsBeforeMatches(): 
    """ 
    Test to ensure players are properly represented in standings prior 
    to any matches being reported. 
    """ 
    deleteMatches() 
    deletePlayers() 
    registerPlayer("Melpomene Murray") 
    registerPlayer("Randy Schwartz") 
    standings = playerStandings() 
    if len(standings) < 2: 
     raise ValueError("Players should appear in playerStandings even before " 
         "they have played any matches.") 
    elif len(standings) > 2: 
     raise ValueError("Only registered players should appear in standings.") 
    if len(standings[0]) != 4: 
     raise ValueError("Each playerStandings row should have four columns.") 
    [(id1, name1, wins1, matches1), (id2, name2, wins2, matches2)] = standings 
    if matches1 != 0 or matches2 != 0 or wins1 != 0 or wins2 != 0: 
     raise ValueError(
      "Newly registered players should have no matches or wins.") 
    if set([name1, name2]) != set(["Melpomene Murray", "Randy Schwartz"]): 
     raise ValueError("Registered players' names should appear in standings, " 
         "even if they have no matches played.") 
    print ("6. Newly registered players appear in the standings with no matches.") 

PostgreSQL模式:

DROP DATABASE IF EXISTS tournament; 
CREATE DATABASE tournament; 
\c tournament; 


CREATE TABLE players (
    id serial PRIMARY KEY NOT NULL, 
    Name text 
); 

CREATE TABLE matches (
    match_id serial PRIMARY KEY NOT NULL, 
    winner int REFERENCES players(id), 
    loser int REFERENCES players(id) 
); 

CREATE VIEW wincounter 
AS 
    SELECT players.id, 
     players.name, 
     COUNT(matches.winner) AS wins 
    FROM players 
     LEFT JOIN matches 
       ON players.id = matches.winner 
    GROUP BY players.id; 

錯誤消息:

從tournament_test.py

def playerStandings(): 
    """Returns a list of the players and their win records, sorted by wins. 

    The first entry in the list should be the player in first place, or a player 
    tied for first place if there is currently a tie. 

    Returns: 
    A list of tuples, each of which contains (id, name, wins, matches): 
     id: the player's unique id (assigned by the database) 
     name: the player's full name (as registered) 
     wins: the number of matches the player has won 
     matches: the number of matches the player has played 
    """ 
    conn = connect() 
    c = conn.cursor() 
    c.execute("SELECT COUNT(id) FROM Players;") 
    total = c.fetchone()[0] 

    num_of_players = countPlayers() 
    standings = [None]*num_of_players 

    c.execute("SELECT * FROM wincounter;") 
    winners = c.fetchall() 

    i = 0 
    for player in winners: 
     standings[i] = (player[0], player[1], player[2], player[3]) 
     i += 1 

    conn.close() 
    return standings 

相關的代碼

[email protected]:/vagrant/tournament$ python tournament_test.py 
    1. countPlayers() returns 0 after initial deletePlayers() execution. 
    2. countPlayers() returns 1 after one player is registered. 
    3. countPlayers() returns 2 after two players are registered. 
    4. countPlayers() returns zero after registered players are deleted. 
    5. Player records successfully deleted. 
    Traceback (most recent call last): 
     File "tournament_test.py", line 152, in <module> 
     testStandingsBeforeMatches() 
     File "tournament_test.py", line 54, in testStandingsBeforeMatches 
     standings = playerStandings() 
     File "/vagrant/tournament/tournament.py", line 85, in playerStandings 
     standings[i] = (player[0], player[1], player[2], player[3]) 
    IndexError: tuple index out of range 
    [email protected]:/vagrant/tournament$ 
+0

當你在'贏家中的球員'後面''和之前和之後簡單地添加'打印播放器'時,你會看到什麼?'[player] [player] [player] [player] ])'?在投擲'IndexError'之前它不打印一些意想不到的東西嗎? – jDo

回答

0

錯誤發生就行了:

standings[i] = (player[0], player[1], player[2], player[3]) 

player變量是代表在數據庫中的wincounter視圖行的元組。該視圖只有三列(players.id,players.namewins),因此當您嘗試訪問第四個值(player[3])時會出現索引錯誤。

這並不完全清楚爲什麼你有這樣的路線。如果你想把player元組中的所有元素都加到standings,你可以做standings[i] = player,甚至可以擺脫顯式循環,只需寫standings = list(winners)

+0

非常感謝,網絡陌生人。 – MrWizard

相關問題