Numbers Game in Python – Lab I

It was cool to handle with python deeper than the famous “Hello World”. I am still getting adapted with the new language, but its looks like not to be hard; No variables declarations, no brackets, no square brackets, just indent.

This is the code of my python game:

#!/usr/bin/python

import random
secret=random.randrange(1,100)

print ‘###########################’
print ‘#           Welcome to guess a number game….. 🙂              #’
print ‘###########################’
print ‘Game Instructions: Your goal is to guess the secret number. ‘
print ‘to play type a number between 1 and 100 and the computer is going to ‘
print ‘say if you have to type a greater number or lower number\n’
print ‘PS: your have a maximum of 10 atempts to guess the number\n’

number_tryes=0
while True:
#Test the limit of the number of tryes
if number_tryes > 10:
break

#Test if the value entered is a number
try:
guess=int(raw_input(“Enter a guess: “))
except:
print ‘You have to type only numbers’
continue
#Test if the number is in the range
try:
if not 0 <= guess <= 100:
continue
except:
print ‘You have to type a numbe between 1 and 100’
continue

if guess<secret:
print “Too low!”
number_tryes+=1
elif guess>secret:
print “Too high!”
number_tryes+=1
else:
number_tryes+=1
print “Correct!\n”
print ‘You typed’, number_tryes , ‘times to guess the magic number’
break
print ‘###########################’

Advertisements

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 )

Google+ photo

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

Connecting to %s


%d bloggers like this: