Powered By Blogger

Monday, July 25, 2011

the journey begins

This was Ben's first day learning Python. He is using 3.1 on OSX 10.6 and working through the book Invent Your Own Computer Games With Python by Al Sweigart. I've asked him to make a note of what he does each day and how long he spent at "Python Camp" that day.

I spent 2 hours.
I learned what the interactive shell is (the code input in python)
and I created my first game (in python that is) by copying from the book: a guess the number game!


# This is a guess the number game.
import random

guessesTaken = 0

print('Hello! What is your name?')
myName = input()

number = random.randint(1, 20)
print('Well, ' + myName + ', I am thinking of a number between 1 and 20.')

while guessesTaken < 6: print('Take a guess.') # There are four spaces in front of print. guess = input() guess = int(guess) guessesTaken = guessesTaken + 1 if guess < number: print('Your guess is too low.') # There are eight spaces in front of print. if guess > number:
print('Your guess is too high.')

if guess == number:
break

if guess == number:
guessesTaken = str(guessesTaken)
print('Good job, ' + myName + '! You guessed my number in ' + guessesTaken + ' guesses!')

if guess != number:
number = str(number)
print('Nope. The number I was thinking of was ' + number)