Need Help! Contact me.Send Feedback Thank You.

Guess the Number – Game in Python

Guess the Number phyton scriptMini Project

Practice Exercise in Python for Beginners.

Create a Guess the Number game where in the computer

One of the simplest two-player games is “Guess the number”. The first
player thinks of a secret number in some known range while the second
player attempts to guess the number. After each guess, the first
player answers either “Higher”, “Lower” or “Correct!” depending on
whether the secret number is higher, lower or equal to the guess. In
this project, you will build a simple interactive program in Python
where the computer will take the role of the first player while you
play as the second player. The player can choose to guess the number either between 0-100 or 0-1000 range. For 0-100 range the player get 7 attempts and for 0-1000 the player gets 10 attempts.

The UI can be command line or graphical with buttons and screen. All the best.

Script

The code is also available at CodeSkulptor.org at the following URL and can be executed as well.

http://www.codeskulptor.org/#user3-w9San7QKx9-11.py

  1. # template for “Guess the number” mini-project
  2. # input will come from buttons and an input field
  3. # all output for the game will be printed in the console
  4. import random
  5. import simplegui
  6. import math
  7. # initialize global variables used in your code
  8. computer_no = 0
  9. upper_range = 100
  10. max_tries = 7
  11. player_attempts = 0
  12. # define event handlers for control panel
  13. def range100():
  14.     global upper_range, max_tries, player_attempts,computer_no
  15.     # button that changes range to range [0,100) and restarts
  16.     print "New game [1-100]“
  17.     upper_range = 100
  18.     max_tries = 7
  19.     player_attempts = 0
  20.     computer_no = random.randrange(0,upper_range)
  21.     #print “computer “,computer_no
  22. def range1000():
  23.     global upper_range, max_tries, player_attempts,computer_no
  24.     print “New game [1-1000]“
  25.     upper_range = 1000
  26.     max_tries = 10
  27.     player_attempts = 0
  28.     computer_no = random.randrange(0,upper_range)
  29.     #print “computer “,computer_no
  30. def get_input(guess):
  31.     global computer_no,player_attempts,max_tries
  32.     # main game logic goes here
  33.     player_attempts = player_attempts + 1
  34.     #print “Comp”, computer_no
  35.     print “You guessed”,guess
  36.     attempt_left = max_tries - player_attempts
  37.     print attempt_left, “attempts left”
  38.     if (attempt_left == 0):
  39.         print “No more tries, the number was”,computer_no
  40.     else:
  41.         guess = int(guess)
  42.         if (guess > computer_no):
  43.             print “Too high!”
  44.             print “===”
  45.         elif (guess < computer_no):
  46.             print “Too low!”
  47.             print “===”
  48.         else:
  49.             print “You got it!”
  50.             print “===”
  51. # create frame
  52. poo = simplegui.create_frame(“guess the number”, 200, 200)
  53. # register event handlers for control elements
  54. poo.add_button(“Start new game [0 - 100]“, range100, 200)
  55. poo.add_button(“Start new game [0 - 1000]“, range1000, 200)
  56. poo.add_input(“Enter your guess”, get_input, 200)
  57. # start frame
  58. poo.start()
  59. # always remember to check your completed program against the grading rubric
Subscribe to DeveloperFeedDid you like this article? Did it Help you Solve your Problem? You can get the all the latest articles published at DeveloperFeed in your email inbox by entering your email address below. Your address will only be used for mailing you the articles, and each one will include a link so you can unsubscribe at any time.