patternpythonMinor
Guessing Game in Python
Viewed 0 times
gamepythonguessing
Problem
Now that I've got this working, any improvements that I can make to better my code review would be helpful.
import random
import math
import console
console.set_font('Arial-BoldMT',15)
print'Welcome to the guessing game!\nA number will be randomly chosen from 0 to 1000.\nThe player will make a guess, and then the computer will guess. Who ever is closest wins that round!\nFirst to 5 wins!'
rounds = 0
run_again = 'y'
player_wins = 0
computer_wins = 0
draws = 0
while run_again == 'y':
number = random.randint(0,1000)
player_number = input('\nPlayer: ')
computer_number = random.randint(0,1000)
print 'Computer:', computer_number
print 'Number:', number
player_score = math.fabs(player_number - number)
computer_score = math.fabs(computer_number - number)
if player_score>=computer_score:
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)
if player_score computer_wins:
console.set_color(0.00, 0.00, 1.00)
print '\nYOU WON THE GAME'
console.set_color(0.00, 0.00, 0.00)
break
elif computer_wins > player_wins:
console.set_color(1.00, 0.00, 0.00)
print '\nYOU LOST THE GAME'
console.set_color(0.00, 0.00, 0.00)
break
else:
print "Wrong counts"Solution
I think the biggest thing here is that you need to create some functions and encapsulate stuff.
Big Points
Specifics
The minimum and maximum can be constants.
... Then, all you have to do is change these variables if you want to modify the game in the future:
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.)
You can have
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:
This will now return
(Note that in your code, it's actually impossible for a draw to occur, since you check both of the winning conditions with
I might even make a
This would reduce our code above to:
(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
This lets you load you
Putting it all together, we get something that looks like this. If it were me, I'd probably further specify the
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 stuffYou 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_scoreThis 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
=, 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_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()Code Snippets
MINIMUM = 0
MAXIMUM = 1000number = random.randint(MINIMUM, MAXIMUM)
player_guess = input('\nPlayer: ')
computer_guess = random.randint(MINIMUM, MAXIMUM)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
// ... then, elsewhere ...
if run_again:
//do stuffdef 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_scoreContext
StackExchange Code Review Q#47086, answer score: 8
Revisions (0)
No revisions yet.