Python Script Guessing Game

This python script is a little guessing game, where you guess a number from 1 – 100

Script white space is broken due to wordpress being… Silly… I will fix it soon.

—————————————————————————————-

#!/usr/bin/env python

# Import the random module
import random
# Create a random number between 1 and 100
secret = random.randrange(1,100)
# Count the score(lower the better)
count = 0

print "Guess a number from 1 to 100"

# This will loop as long as secret is equal to anything
while secret:
    # Increment score
    count += 1
    # Try to something, if there is an error raise exception
    try:
        # Get input from user and check if it is a number between 1 and 100. If you guess correct it will let you know and end the loop
        guess = int(raw_input("Enter a guess: "))
        if guess < 100 and guess > 1:
            if guess < secret:
                print "Too low!"
            elif guess > secret:
                print "Too high!"
            else:
                print "Correct! Guesses taken: " + str(count)
                secret  = None
        else:
            raise Exception()
    except:
        print "Error: invalid data - Please input a integer between 1 and 100"

—————————————————————————————-

Advertisements

About oatleywillisa

Computer Networking Student
This entry was posted in SBR600. Bookmark the permalink.

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