In the following program, why do I need to use the name "player" rather than a regular counter like "i"? I ran the program this way and it prints out the last element in players for how ever many player elements are in players. I want to know what's going on behind the scenes, why is it choosing only the last element. Thanks.

# Simple Game
# Demonstrates importing modules

import games, random

print "Welcome to the world's simplest game!\n"

again = None

while again != "n":
    players = []
    num = games.ask_number(question = "How many players? (2 - 5): ",
                           low = 2, high = 5)
    
    for i in range(num):
        name = raw_input("Player name: ")
        score = random.randrange(100) + 1
        player = games.Player(name, score)
        players.append(player)
        
    print "\nHere are the game results:"
        
    for player in players:
        print player
            
    again = games.ask_yes_no("\nDo you want to play again? (y/n): ")
        
raw_input("\n\nPress the enter key to exit.")

Recommended Answers

All 3 Replies

while (raw_input("Player name: ")):

is this work?

Editors note: maybe signature spam

The things you are asking for are part of module games. You have to investigate the source of that module.

In line ...
player = games.Player(name, score)
player is the instance of class Player in module games,
but you can't be sure unless you check the source of the module.

My assumption as the class Player uses __str__() to override str() and hence print of the instance.

You can change player to i if you want. Doesn't matter. You can change it to asdfjklkansns if you want to.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.