I think the biggest thing here is that you need to create some functions and encapsulate stuff.
Big Points
- Use reusable functions, especially when you find yourself copy-and-pasting code.
- Don't hardcode anything; use constants rather than "magic numbers".
- Don't use global variables.
- Wrap it in a
main()
.
Specifics
The minimum and maximum can be constants.
MINIMUM = 0
MAXIMUM = 1000
... Then, all you have to do is change these variables if you want to modify the game in the future:
number = random.randint(MINIMUM, MAXIMUM)
player_guess = input('\nPlayer: ')
computer_guess = random.randint(MINIMUM, MAXIMUM)
Break up the big multi-line message at the beginning rather than having it stretch on for 200 characters. (Purists will still say that the implemenation below has the second line too long, as the Python standard is to have no line exceeding 80 characters, but you get the idea.)
print 'Welcome to the guessing game!'
print 'A number will be randomly chosen from ' + str(MINIMUM) + ' to ' + str(MAXIMUM)
print 'The player will make a guess, and then the computer will guess.'
print 'Whoever is closest wins that round!'
print 'First to 5 wins!'
You can have run_again
directly store whether or not to run again (i.e., a boolean value) rather than a character. This lets you check directly against it:
run_again = True
// ... then, elsewhere ...
if run_again:
//do stuff
You can encapsulate the entire "guessing round" of the game into its own function which then returns whether or not the player won. This will let you have a bit more control over your code structure:
def perform_guessing(number):
player_guess = input('\nPlayer: ')
computer_guess = random.randint(MINIMUM, MAXIMUM)
print 'Computer: ' + str(computer_guess)
print 'Number: ' + str(number)
player_score = math.fabs(player_guess - number)
computer_score = math.fabs(computer_guess - number)
return player_score < computer_score
This will now return True
if the player has won the round, so we can use it as such:
while run_again:
if perform_guessing(random.randint(MINIMUM, MAXIMUM)):
player_wins += 1
console.set_color(0.00, 0.00, 1.00)
print 'You won that round'
console.set_color(0.00, 0.00, 0.00)
else:
computer_wins += 1
console.set_color(1.00, 0.00, 0.00)
print 'You lost that round'
console.set_color(0.00, 0.00, 0.00)
(Note that in your code, it's actually impossible for a draw to occur, since you check both of the winning conditions with <=
and >=
, meaning that the ==
condition will be caught in one of those. This may be a bug in your program. :) )
I might even make a color_print
function to avoid some copy-and-pasting:
def color_print(text, r, g, b):
console.set_color(r, g, b)
print text
console.set_color(0.00, 0.00, 0.00)
This would reduce our code above to:
while run_again:
if perform_guessing(random.randint(MINIMUM, MAXIMUM)):
player_wins += 1
color_print('You won that round!', 0.0, 0.0, 1.0)
else:
computer_wins += 1
color_print('You lost that round.', 1.0, 0.0, 0.0)
(I don't actually know what the three decimal values represent when you're setting the console color. If they're not RGB, you can rename them accordingly. But you get the idea.)
Finally, it's standard to encapsulate your functionality into a def main()
and call it like so:
if __name__ == '__main__':
main()
This lets you load you import
things from this file into another file later without it actually executing.
Putting it all together, we get something that looks like this. If it were me, I'd probably further specify the color_print
function so that you could pass in something like Color.RED
or Color.YELLOW
rather than the three decimals, but that can be for a future release, haha. There's some further encapsulation you could do to make the code still more elegant and readable, but I leave that as an exercise to the reader.
import random
import math
import console
MINIMUM = 0
MAXIMUM = 1000
FONT = 'Arial-BoldMT'
FONT_SIZE = 15
def perform_guessing(number):
player_guess = input('\nPlayer: ')
computer_guess = random.randint(MINIMUM, MAXIMUM)
print 'Computer: ' + str(computer_guess)
print 'Number: ' + str(number)
player_score = math.fabs(player_guess - number)
computer_score = math.fabs(computer_guess - number)
return player_score < computer_score
def color_print(text, r, g, b):
console.set_color(r, g, b)
print text
console.set_color(0.00, 0.00, 0.00)
def main():
console.set_font(FONT, FONT_SIZE)
print 'Welcome to the guessing game!'
print 'A number will be randomly chosen from ' + str(MINIMUM) + ' to ' + str(MAXIMUM)
print 'The player will make a guess, and then the computer will guess.'
print 'Whoever is closest wins that round!'
print 'First to 5 wins!'
run_again = True
player_wins = 0
computer_wins = 0
rounds = 0
while run_again:
rounds += 1
if perform_guessing(random.randint(MINIMUM, MAXIMUM)):
player_wins += 1
color_print('You won that round!', 0.0, 0.0, 1.0)
else:
computer_wins += 1
color_print('You lost that round.', 1.0, 0.0, 0.0)
if rounds == 5:
if player_wins > computer_wins:
color_print('YOU WON THE GAME!', 0.0, 0.0, 1.0)
else:
color_print('YOU LOST THE GAME', 1.0, 0.0, 0.0)
rounds = 0
player_wins = 0
computer_wins = 0
if __name__ == '__main__':
main()