0

I am creating a 'Guess The Number' game, and am having issues when attempting to run it. The error I get is as follows:

Traceback (most recent call last):
File "C:\Users\Troy\Desktop\guess.py", line 10, in
begin()
File "C:\Users\Troy\Desktop\guess.py", line 9, in begin
ask()
NameError: name 'ask' is not defined

In the different 'chunks' I have defined the script in, here it is:


The first part here defined as begin() thinks of a number, and asks tells the user that it is thinking of a number between 1 and 10.

def begin():
    import random
    import sys
    guessesRemaining = 3
    randomNumber = random.randint(1,10)
    print("I am thinking of a number between 1 and 10.")
    ask()
begin()

The next part is defined as ask() and asks the user to enter a number that they guess, as long as they have enough guesses remaining.

def ask():
    if guessesRemaining == 0:
        print("Oh no! You've run out of guesses! I was thinking of the number " + str(randomNumber) + ".")
    else:
        print ("Take a guess.")
        guess = input(">> ")
        if int(guess) < randomNumber:
            print("Your number is too small!")
            global guessesRemaining
            guessesRemaining -= int(1)
            ask()
        elif int(guess) > randomNumber:
            print("Your number is too big!")
            global guessesRemaining
            guessesRemaining -= int(1)
            ask()
        elif int(guess) == randomNumber:
            print("Well done! You got the right number!")
            playAgain()
ask()

And this final part is defined as playAgain() which asks the user whether they want to play again.

def playAgain():
    print("Would you like to play again?")
    again = input
    if again == y or Y or yes or Yes:
        print("Restarting game...")
        begin()
    if again == n or N or no or No:
        print("Quitting game...")
        sys.quit()
    else:
        print("Invalid response!")
        playAgain()
playAgain()
Elazar
  • 20,415
  • 4
  • 46
  • 67
Troy Cooper
  • 3
  • 1
  • 6

3 Answers3

3

You can't call ask until ask has been defined.

Here, you call the begin function immediately after it has been defined:

def begin():
    import random
    import sys
    guessesRemaining = 3
    randomNumber = random.randint(1,10)
    print("I am thinking of a number between 1 and 10.")
    ask()
begin() # this calls begin immediately!

Notice that within begin, there is a call to ask, but by this point the ask function still doesn't yet exist!

You probably want to put the calls to begin, ask and playAgain all the way at the very end of the script, at least. Something like this:

def begin():
    ...
def ask():
    ...
def playAgain():
    ...

begin()
ask()
playAgain()

Now, even with this change, your program is not doing what you think is doing. There are several problems:

  • You don't need to call begin or ask, actually, because they are implicitly called by playAgain.
  • The variable guessesRemaining and randomNumber are both local to the function, hence whenever you call another function they will see not see the same variable at all. To fix this you can make the variables global:

    def begin():
        global guessesRemaining, randomNumber
        ...
    

    And similarly for ask.

Rufflewind
  • 8,545
  • 2
  • 35
  • 55
0

You need to define ask() before calling it, just like you did with begin(). Python is an interpreted language, so if a function is called above its definition, the interpreter won't recognize it.

TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97
0

In your example, you call begin() before ask() has been defined, so when begin tries to call it, it's undefined.

You normally want to define all of your functions before invoking any of them. Otherwise you will start executing your program before all of your functions are available. Just move your call to the bottom of your file and everything will be defined when it's called.

Tom Karzes
  • 22,815
  • 2
  • 22
  • 41