Archive for April, 2009

My First Game!

April 4, 2009

I have  finally written my first game. It is not the funnest game you will ever play. It is a simple number guessing game. I made it because my brother said “It’s just a few easy lines of code”. It turned out to be a two-and-a-half-day project. We started out with learning the raw_input builtin. That made a few things MUCH easier. Then we added a few if statements to tell you whether you should guess higher or lower or  you got it right. When we finally figured all of that out we decided to put in a loop so that you don’t have to keep running the code until you got it right. We used a while loop and talked about the differences between the while loop and the for loop. When we figured that out then we had to do a lot of editing because my indentions suck so it was causing problems with the code. Then we noticed that it didn’t tell you when you won, it just stopped. So we fixed that by taking the if statement out of the loop and put it after the loop. Now it works!

import random
compnumber =  random.randint(1,  100)
usrnumber = 0
while usrnumber != compnumber:
usrnumber = raw_input(‘Your number please:’)
usrnumber = int(usrnumber)

if usrnumber > compnumber:
print “Too high!”
if usrnumber < compnumber:
print “Not quite high enough…”

if usrnumber == compnumber – 1:
print “So close…”
if usrnumber == compnumber + 1:
print “So close…”
print “Yay! You did it!”