Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Can not assign value to a variable

def simulate_tournament(teams):
    """Simulate a tournament. Return name of winning team."""
    # If only one team left
    if len(teams) == 1:
        # Assigns the only element of type dictionary of the list to a variable
        winner = teams[0]
        # Gets value of the firs key(name a team)
        i = list(winner.values())[0]
        # Returns name of the team type <'class str'>
        return i

    else:
        # Calls function to simulate a game between each pair of teams and returns list of winners
        winners = simulate_round(teams)
        # Using recursion to simulate all games in order to get the base case
        simulate_tournament(winners)

Example of a dictionary element

team = {
    "name": "Germany"
    "rating": 1000
}

But when I try no assign a return value to a variable in my main function and try to print it:

winner = simulate_tournament(teams)
print(winner)

It prints None

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You need to return your recursive call. Without return, you’re just calling the function and discarding the value returned.

def simulate_tournament(teams):
    """Simulate a tournament. Return name of winning team."""
    # If only one team left
    if len(teams) == 1:
        # Assigns the only element of type dictionary of the list to a variable
        winner = teams[0]
        # Gets value of the firs key(name a team)
        i = list(winner.values())[0]
        # Returns name of the team type <'class str'>
        return i

    else:
        # Calls function to simulate a game between each pair of teams and returns list of winners
        winners = simulate_round(teams)
        # Using recursion to simulate all games in order to get the base case
        return simulate_tournament(winners)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading