-
Notifications
You must be signed in to change notification settings - Fork 1
/
guess_the_number.py
executable file
·58 lines (48 loc) · 1.52 KB
/
guess_the_number.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
"""
Name: Guess the Number mini-project.
Author: jraleman
Year: 2013
"""
try:
import simplegui
except ImportError:
import SimpleGUICS2Pygame.simpleguics2pygame as simplegui
import random
# Global variables for the answer and the number of guesses.
ANSWER = 0
GUESSES = 7
def new_game(low, high):
"""
Helper function to start and restart the game.
"""
global ANSWER, GUESSES
ANSWER = random.randrange(low, high)
if (GUESSES > 0):
print "Guess a number from", low, "to", high
print "Try to guess the number! You have", GUESSES, "guesses."
def input_guess(guess):
"""
Main game logic goes here.
"""
global GUESSES
GUESSES -= 1
number_guess = int(guess)
# When the answer and the guess are the same, the player wins.
if number_guess == ANSWER:
print "Your guess:", guess, "is correct! Congratulations! :D"
# Indicate the player if the ANSWER is lower or higher.
elif GUESSES > 0:
print "Your guess was:", guess
if number_guess > ANSWER:
print "Try guessing a LOWER number!"
if number_guess < ANSWER:
print "Try guessing a HIGHER number!"
print "Guesses remaining:", GUESSES
# Game over condition, shows the ANSWER.
else:
print "Game over! You ran out of guesses. Why don't you try again?"
print "The answer was:", ANSWER
# Get things rolling.
new_game(0, 100)
while(GUESSES):
input_guess(raw_input("Guess: "))