0

For Lab6 the code I have written seems to have problems running. Even with check50 it tells me that "check50 is taking longer than usual". I am unable to figure out what is wrong with my code.

   # Simulate a sports tournament

import csv
import sys
import random


# Number of simluations to run
N = 1000


def main():

    # Ensure correct usage
    if len(sys.argv) != 2:
        sys.exit("Usage: python tournament.py FILENAME")

    teams = []
    # TODO: Read teams into memory from file
    filename= sys.argv[1]
    with open(filename) as csvdog:
        reading = csv.DictReader(csvdog)
        for row in reading:
            team= row
            team["rating"]= int(team["rating"])
            teams.append(team)






    counts = {}
    # TODO: Simulate N tournaments and keep track of win counts

    for times in range(N):
        simulate_tournament(teams)
        if winner in counts:
            counts[winner]+=1
        else:
            counts[winner]=1

    # Print each team's chances of winning, according to simulation
    for team in sorted(counts, key=lambda team: counts[team], reverse=True):
        print(f"{team}: {counts[team] * 100 / N:.1f}% chance of winning")


def simulate_game(team1, team2):
    """Simulate a game. Return True if team1 wins, False otherwise."""
    rating1 = team1["rating"]
    rating2 = team2["rating"]
    probability = 1 / (1 + 10 ** ((rating2 - rating1) / 600))
    return random.random() < probability


def simulate_round(teams):
    """Simulate a round. Return a list of winning teams."""
    winners = []

    # Simulate games for all pairs of teams
    for i in range(0, len(teams), 2):
        if simulate_game(teams[i], teams[i + 1]):
            winners.append(teams[i])
        else:
            winners.append(teams[i + 1])

    return winners


def simulate_tournament(teams):
    """Simulate a tournament. Return name of winning team."""
    # TODO
    while len(teams)>1:
        simulate_round(teams)
    return teams[0]["teams"]



if __name__ == "__main__":
    main()

Any help would be greatly appreciated!

0

In this case, 'longer than usual' means 'infinite loop'.
Take a look at simulate_tournament. It's called with the input teams, which is then passed to simulate_round which has a return value. What happens to that return value? When teams is evaluated for the while loop condition, it's exactly the same as the first time.
Another indication that the program never gets to the return statement in 'simulate_tournament' is that there's a typo. return teams[0]["teams"] should be["team"].

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .