-2

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?

TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97
Dev.Ays
  • 1
  • 5
  • And before you thumb down my question please keep in mind that I AM LEARNING. i just don't understand the arrogance of a lot of this community toward people trying to put in the work to learn. – Dev.Ays Mar 28 '17 at 22:21
  • 1
    Donating time to help other people avoid wasting their time with low-quality questions isn't arrogance, it's generosity. – TigerhawkT3 Mar 28 '17 at 22:22
  • Yeah, sometimes, I don't understand duplicates like this. He clearly doesn't understand how his code fits into the answer given on the other question. – meyer9 Mar 28 '17 at 22:22
  • What other question? how is this a duplicate? please explain. again im trying to learn and im being kicked for it. its kind of ridiculous. I came here for expertise, not to be made fun of for my lack of knowledge. – Dev.Ays Mar 28 '17 at 22:26
  • @meyer9 - It's a perfect duplicate. The answer clearly demonstrates that when you indent too much, you get an error. It's exactly what's happening here. – TigerhawkT3 Mar 28 '17 at 22:27
  • What book are you using? It's entirely possible that the code you carefully followed had an error. Every Python book or tutorial I've seen other than the [official Python tutorial](https://docs.python.org/3.6/tutorial/index.html) is pretty bad. – TigerhawkT3 Mar 28 '17 at 22:28
  • I understand it's a duplicate, but I feel like the asker doesn't understand how their code fits in with the duplicate question. It's very probable OP has already seen that question, but does not understand how to use it. I think it is still very useful to help someone fit concepts into their specific instance - especially for beginner programmers. – meyer9 Mar 28 '17 at 22:29
  • But when I indent that line as well, I get a whole other error, attribute error, saying that module has no attribute 'ask_number' but i did define this in the module didn't I? I am learning from a textbook and so this is why I am asking. I simply want to know what is going wrong, but indenting the line that I have been instructed to indent causes this other error. – Dev.Ays Mar 28 '17 at 22:31
  • @meyer9 - "I understand it's a duplicate" is the only criterion for a question being a duplicate. Tutorial services are off-topic for SO. – TigerhawkT3 Mar 28 '17 at 22:31
  • The book is "Python Programming for the Absolute Beginner" by Michael Dawson. the module can be found at courseptr.com/downloads. go left taskbar, go to programming, find the name of the book, click downloads, doanload the source code for the book. open up chapter 9 folder. – Dev.Ays Mar 28 '17 at 22:35
  • 2
    @Dev.Ays: You get a whole different error, because you have more than one bug. Fixing the indentation only fixes one issue. We're not here to completely fix your code in every way, or even to get rid of every error message. We provide answers to questions. We've provided one. – user2357112 Mar 28 '17 at 22:37
  • you're right i will search myself, thanks guys – Dev.Ays Mar 28 '17 at 22:44

1 Answers1

-1

This line: num = games.ask_number(question = "How many players? (2 - 5): ", low = 2, high = 5) should be indented. I suggest you look into a basic Python tutorial. Essentially, every time you type : and start writing code on the next line, you need to increase your indentation level.

meyer9
  • 1,120
  • 9
  • 26
  • when i did that, this error now happens upon program execution: Traceback (most recent call last): File "C:\Users\Devin\Desktop\Beginner Python Files\Simple Game.py", line 11, in num = games.ask_number(question = "How many players? (2 - 5): ", low = 2, high = 5) AttributeError: 'module' object has no attribute 'ask_number' but this isn't true as the attribute is indeed defined? – Dev.Ays Mar 28 '17 at 22:23
  • How are you defining it in that module? – meyer9 Mar 28 '17 at 22:25
  • 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 It is this section in the module. so what am I doing wrong? This is verbatim from the textbook. – Dev.Ays Mar 28 '17 at 22:27
  • and that's in a module named games.py in the same directory? – meyer9 Mar 28 '17 at 22:29
  • 2
    `games` doesn't have an `ask_number`. It has `Player`. It is `Player` that has `ask_number`. You probably want to unindent everything after `Player`'s second method. – TigerhawkT3 Mar 28 '17 at 22:30
  • I'm not sure why people are downvoting my clear answer to the original question. – meyer9 Mar 28 '17 at 22:32
  • Possibly because answers to duplicate questions about very basic concepts aren't useful. – TigerhawkT3 Mar 28 '17 at 22:33
  • It was clearly useful to the original poster. The question is already closed, so although it may not be useful to everyone, it doesn't particularly matter. – meyer9 Mar 28 '17 at 22:34
  • yes it is all in the same directory. – Dev.Ays Mar 28 '17 at 22:36
  • TIGERHAWK ON THE MONEY. in the module games, in class Player, i indented all of functions under class Player, not understanding that after the second, method, that all of the following functions were not a part of that class. thank you TIGER, reading another post would have not helped me find this, only the expertise of the community, which is why i asked you guys. thank you. and now this will never happen again. see? was it so bad i asked? im not trying to waste time guys, just trying to learn. – Dev.Ays Mar 28 '17 at 22:38
  • apparently, people are recommending to do this in a private chat room next time, which I will most definitely do. thanks @TigerhawkT3 – meyer9 Mar 28 '17 at 22:41
  • Although i followed your advice Tiger, same indent error in the same line. when i follow the advice of the posted error, to indent the "num =" line the program runs, with no error, but does nothing near what it is supposed to. although thank you for pointing out the clear discrepancies in my module. – Dev.Ays Mar 28 '17 at 22:44
  • 1
    @Dev.Ays - The term that fits the definition of what you are seeking is "tutoring." This site does not aim to provide that. SO's goal is to build a database of useful questions and answers. A question like this one doesn't match that goal. If you need personalized help beyond the resources available to you (tutorials, documentation, etc.), you will need to hire a tutor. Since a tutor's efforts only help one person rather than build a QA database, they'll charge you (around $40-100 per hour, usually). – TigerhawkT3 Mar 28 '17 at 22:44
  • Okay, i get what you're getting at, I apologize. – Dev.Ays Mar 28 '17 at 22:45