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.")

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

is this work?

Editors note: maybe signature spam

Edited 6 Years Ago by vegaseat: sig 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.

Edited 6 Years Ago by vegaseat: class

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

This article has been dead for over six months. Start a new discussion instead.