patternpythonMinor
Number-guessing game with maximum and minimum values
Viewed 0 times
numbermaximumvalueswithminimumgameguessingand
Problem
This is a simple guessing game where you are prompted for a minimum and maximum value, then you must guess the number the computer chooses! This is the first program I have written so any and all suggestions are welcome.
def guessinggame():
print "Welcome to the Guessing Game!."
while True:
try:
numrange1 = int(raw_input("What is the minimum number you want?: "))
break
except ValueError:
print "This must be an integer!"
while True:
try:
numrange2 = int(raw_input("What is the maximum number you want?: "))
break
except ValueError:
print "This must be an integer!"
rnumber = random.randint(numrange1, numrange2)
i = 0
while True:
x = int(raw_input("What is your guess?: "))
if rnumber > x:
print "Low!"
i = i + 1
elif rnumber < x:
print "High!"
i = i + 1
elif rnumber == x:
print "Correct!"
i = i + 1
print i
break
again = raw_input("Would you like to play again? (y/n): ")
if again == 'y':
guessinggame()
guessinggame()Solution
You are inappropriately using the guessinggame() function as if it were a goto label. After playing the game repeatedly, you will end up with a deep stack trace (which you can see by hitting ControlC), which is a bad sign. A proper design would be:
Curiously, you check that the bounds are integers, but you don't bother to validate the guesses. You also don't check that the minimum is less than the maximum.
All three prompts would benefit from a shared integer-prompting routine:
Getting that clutter out of the way makes your main function more readable:
I've renamed
def guessing_game():
# Code to play one game
play = True
while play:
guessing_game()
play = ('y' == raw_input('Would you like to play again? (y/n): '))Curiously, you check that the bounds are integers, but you don't bother to validate the guesses. You also don't check that the minimum is less than the maximum.
All three prompts would benefit from a shared integer-prompting routine:
def prompt_int(prompt, err_msg='This must be an integer!'):
while True:
try:
return int(raw_input(prompt))
except ValueError:
print err_msgGetting that clutter out of the way makes your main function more readable:
def guessing_game():
print "Welcome to the Guessing Game!."
rnumber = None
while rnumber is None:
lower = prompt_int('What is the minimum number you want?: ')
upper = prompt_int('What is the maximum number you want?: ')
if lower rnumber:
print "High!"
elif guess == rnumber:
print "Correct!"
print tries
breakI've renamed
numrange1 to lower, numrange2 to upper, i to tries, and x to guess. Incrementing the loop counter is common to all three branches and can be factored out.Code Snippets
def guessing_game():
# Code to play one game
play = True
while play:
guessing_game()
play = ('y' == raw_input('Would you like to play again? (y/n): '))def prompt_int(prompt, err_msg='This must be an integer!'):
while True:
try:
return int(raw_input(prompt))
except ValueError:
print err_msgdef guessing_game():
print "Welcome to the Guessing Game!."
rnumber = None
while rnumber is None:
lower = prompt_int('What is the minimum number you want?: ')
upper = prompt_int('What is the maximum number you want?: ')
if lower < upper:
rnumber = random.randint(lower, upper)
else:
print "The minimum must be less than the maximum!"
tries = 0
while True:
guess = prompt_int("What is your guess?: ")
tries += 1
if guess < rnumber:
print "Low!"
elif guess > rnumber:
print "High!"
elif guess == rnumber:
print "Correct!"
print tries
breakContext
StackExchange Code Review Q#69333, answer score: 3
Revisions (0)
No revisions yet.