This is my second program in Python. I am aware that globals are a bad idea, but that is what I have learned so far in my course.
Right now the program is doing what it's supposed to do, my only concern is that it might be too long for nothing. I'm asking you to help me figure out where I could make it simpler.
import simplegui
import random
import math
#Globals, random guess and player's remaining guesses
num_range = 100
remaining_guesses = 7
# New game, range from 0 - 100 by default
# after the according button is pressed
# Reset the remaining guesses to 7
def range100():
global num_range
global remaining_guesses
remaining_guesses = 7
print "New game. Range is from 0 to 100"
print "Number of remaining guesses is %i" %(remaining_guesses)
print ""
num_range = random.randrange(0, 101)
return num_range
# Set game to a range of 0 - 1000
# after the according button is pressed
# Reset the remaining guesses to 10
def range1000():
global num_range
global remaining_guesses
remaining_guesses = 10
print "New game. Range is from 0 to 1000"
print "Number of remaining guesses is %i" %(remaining_guesses)
print ""
num_range = random.randrange(0, 1001)
return num_range
# Compare guess to random range
# Remove 1 to remaining guesses
# Display hint on the random range
# Restart the game if player is correct or out of guesses
def input_guess(guess):
guess = float(guess)
global remaining_guesses
global num_range
remaining_guesses -= 1
print "Your guess was: %i" %(guess)
print "Number of remaining guesses is %i" %(remaining_guesses)
if (remaining_guesses == 0 and guess != num_range):
print "The correct answer was %i" %(num_range)
print "Let's try again!"
print ""
print ""
range100()
elif (remaining_guesses == 0 and guess == num_range):
print "Correct!"
print ""
print ""
range100()
elif guess > num_range:
print "Try a lower number!"
elif guess < num_range:
print "Try a higher number!"
else:
print "Correct!"
print ""
print ""
range100()
print ""
return guess, remaining_guesses
# Create & start frame
f = simplegui.create_frame("Guess the number", 200, 200)
f.add_button("Range is [0, 100]", range100, 200)
f.add_button("Range is [0, 1000]", range1000, 200)
f.add_input('My label', input_guess, 50)
f.start()
# Start a new game
range100()
1 Answer 1
You have a function called input_guess()
. That function inputs the guess, compares the guess, and plays the entire game. It should only input the guess. I would create a function to run the program, one to input the number, and one to do the comparison and output prompts about being too large or small. This way, only the function that plays the game needs to know the number of moves left. Once the number is correctly guessed, that function just returns to its caller, who can call it again if the user wants to play again. Also, try not to write functions this long.
As for your range100()
and range1000()
functions, I would combine those into one function like this:
def get_num(min, max):
print "New game. Range is from " + min + " to " + max
num = random.randrange(min, max + 1)
return num
You can tell the user how many guesses they have left in the function that controls the game.
Finally, you should have the part of the program that always runs in an if __name__ == "__main__":
block. This is for re-usability of your program, and you can read more about it here: What does if __name__ == "__main__":
do?.
Explore related questions
See similar questions with these tags.
simplegui
. The code in this question runs on CodeSkulptor, not the simplegui that acts as a wrapper for Tkinter. \$\endgroup\$