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()