GUESS THE NUMBER GAME

It requires Python 3.0. This scripts gets a random integer number between 1 and 100 and you have 10 tries to find it.
After each attempt, you'll know if the number is bigger or smaller, until you'll guess it.

#!/usr/bin/env python3.0
#
# guess_the_number.py
#     
# Cristian Navalici ncristian at lemonsoftware.eu  
#
# guess a radomly chosen number within 10 tries.
 
import random
 
def main():
    number  = random.randint(1, 100)
    tries   = 10
    guessNr = 1
 
    while tries:
        print('Try: ', guessNr)
        guess = int(input('Enter an integer between 1-100 : '))
        if guess == number:
            print('Congratulations, you guessed it.')
            tries = 0 # this causes the while loop to stop
        elif guess < number:
            print('Try a little higher.')
        else:
            print('Try a little lower.')
 
        guessNr += 1
    else:
        print('The guessing is over. The number was', number)
 
 
 
if __name__ == '__main__': main()

 
python/py3guessthenumber.txt · Last modified: 2011/11/25 10:45 (external edit)
Email address
 
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki