Small game in python

Hi..today i found in a site a small guessing game in python..

here the game codes

 

Guess.py

  1. # This is a guess the number game.
  2. import random
  3. guessesTaken = 0
  4. print(‘Hello! What is your name?’)
  5. myName = input()
  6. number = random.randint(1, 20)
  7. print(‘Well, ‘ + myName + ‘, I am thinking of a number between 1 and 20.’)
  8. while guessesTaken < 6:
  9.     print(‘Take a guess.’) # There are four spaces in front of print.
  10.     guess = input()
  11.     guess = int(guess)
  12.     guessesTaken = guessesTaken + 1
  13.     if guess < number:
  14.         print(‘Your guess is too low.’) # There are eight spaces in front of print.
  15.     if guess > number:
  16.         print(‘Your guess is too high.’)
  17.     if guess == number:
  18.         break
  19. if guess == number:
  20.     guessesTaken = str(guessesTaken)
  21.     print(‘Good job, ‘ + myName + ‘! You guessed my number in ‘ + guessesTaken + ‘ guesses!’)
  22. if guess != number:
  23.     number = str(number)
  24.     print(‘Nope. The number I was thinking of was ‘ + number)

 

 

run the file by python Guess.py

its nice to learn python by playing with small games like this..

Author: Balaji

Hi..My name is Balaji and i am working as a Senior Software Developer in India. I am interested in Shell scripts, python, erlang , linux kernel .

One thought on “Small game in python”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: