2016-11-27 99 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

相关问题