I am going through a textbook and learning Python very successfully; more successfully than I would have imagined. While going through the current chapter of the book I am on, I have been instructed to create a module "games" to use for import in a program "Simple Game". I have done everything to the T that the book has said to do, yet I am constantly arriving at an indent error. I am not claiming that the author of this book has screwed up (since he is a lot more experienced than I), but I have done everything to the letter of instruction and it is not working correctly whatsoever.I have tried unindenting then unindenting the following lines accordingly,indenting the line above the error, and various other things, and I cant get this to work correctly. Please help.
Here's the "games" module:
class Player(object):
""" A player for a game. """
def __init__(self, name, score = 0):
self.name = name
self.score = score
def __str__(self):
rep = self.name + ":\t" + str(self.score)
return rep
def ask_y_n(question):
""" Ask a yes or no question. """
response = None
while response not in ("y", "n"):
response = input(question).lower()
return response
def ask_number(question, low, high):
""" Ask for a number within a range. """
response = None
while response not in range(low, high):
response = int(input(question))
return response
if __name__ == "__main__":
print("You ran this module directly (and did not 'import' it).")
input("\n\n\t\t\tPress the ENTER key to exit.")
And next i will show you the code of the program that utilizes my created "games" module:
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 = 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_y_n("\nDo you want to play again? (y/n): ")
input("\n\n\t\t\tPress the ENTER key to exit.")
The indentation error occurs in the main program at the point where it says: for i in range(num). that line is getting the error, but why?