I would refactor your code quite a bit, for example place Game Over in it's own function.
The reason you ask another question is you are decrementing the number of guesses after input, but not checking this before getting the next guess from the user. One quick fix would be to check the number of guesses after validating their answer:
def game_over():
print "Game Over! Out Of Guesses!"
new_game();
def input_guess(guess): # main game logic goes here global number_of_guesses guess = int(guess) print "" print "Guess was: ", guess print ""
if guess > secret_number:
print "Lower!"
print ""
number_of_guesses -= 1
elif guess < secret_number:
print "Higher!"
print ""
number_of_guesses -= 1
elif number_of_guesses > 0 and guess == secret_number:
print "Correct!"
new_game()
if number_of_guesses == 0:
game_over();
else:
print "Guesses Remaining: ", number_of_guesses
print ""
else:
print "S**t, something went wrong here."
Still very messy, and untested but hopefully the logic makes sense
manpreet
Best Answer
2 years ago
I've got a small little project for a coursera course and we have to design a "Guess The Number Game". We've also been instructed to give 7 guesses if the number is 1-100 and 10 guesses if it is 1-1000. My code is below. I'm having some issues with my if statements. When guesses == 0, it still makes the user enter another input before telling him the game is over and starting a new game. I need it to say game over and reset when guesses == 0, without making the user guess again. Where am I going wrong here? I've tried setting elif number_of_guesses == 0 as well, with the same issue. Thanks in advance!