Well, I’ve finished reading through “How to Think Like a Computer Scientist,” and I think I’m getting a pretty good handle on the python programming language. I still need to do a lot more work on GUI and I need to start figuring out graphics and sound. Anyway, I sat down yesterday with the PyGame tutorial (yes, I know it’s actually written for kids but hey that just makes it easy to follow), and the first few program examples are straight out of the tutorial, but the Math Tutor I didn’t draw from the tutorial even though I think there might be something similar in it.
#————-
# Name: Guess The Number from PyGame Tutorial
# Purpose: Learning to program
#
# Author: Liam P Boyle
#
# Created: 05/05/2011
# Copyright: (c) Liam P. Boyle 2011
# Licence: <your licence>
#————-
#!/usr/bin/env python
def main():
## 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”)
guess = input()
guess = int(guess)
guessesTaken = guessesTaken + 1
if guess < number:
print(“Your guess is too low”)
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 + ” tries.”)
if guess != number:
number = str(number)
print (“No. The number I was thinking of was ” + number)
if __name__ == ‘__main__’:
main()
## End
Just for fun (Yes, I remember the Zork games and choose your own adventure books)
#————-
# Name: Dragon Realm from the PyGame tutorial
# Purpose: Learning to program
#
# Author: Liam P Boyle
#
# Created: 06/05/2011
# Copyright: (c) Liam P Boyle 2011
# Licence: <>
#————-
#!/usr/bin/env python
#————-
## Import Statements
import random
import time
##————-
## Global Variable Initializatins and Function definitions
def displayIntro():
print (“You are on a planet full of dragons. In front of you,”)
print (“you see two caves. In one cave the dragon is friendly”)
print (“and will share his treasure with you. The other dragon”)
print (“is greedy and hungry, and will eat you on sight.\n”)
def chooseCave():
cave = “”
while cave != “1″ and cave != “2″:
print (“Which cave will you go into (1 or 2)”)
cave = input()
return cave
def checkCave(chosenCave):
print (“You approach the cave…”)
time.sleep(2)
print (“It is dark and spooky…”)
time.sleep(2)
print(“A large dragon jumps out in front of you! He opens his jaws and…”)
print()
time.sleep(2)
friendlyCave = random.randint(1,2)
if chosenCave == str(friendlyCave):
print(“He gives you his treasure.”)
else:
print(“He gobbles you up!”)
#————-
## Main Program Logic
def main():
playAgain = “yes”
while playAgain == “yes” or playAgain == “y”:
displayIntro()
caveNumber = chooseCave()
checkCave(caveNumber)
prompt = “Do you want to play again: yes/no?”
playAgain = input(prompt)
if __name__ == ‘__main__’:
main()
##End
#————-
And finally, my own work:
#——————————————————————————-
# Name: Math Tutor pre-K
# Purpose: help children learn addition and subtraction
#
# Author: Liam P. Boyle
#
# Created: 06/05/2011
# Copyright: (c) Liam P. Boyle 2011
# Licence: <freeware w/ author attribution>
#——————————————————————————-
#!/usr/bin/env python
##Import Statements
import random
#——————————————————————————-
def greeting():
print (“What is your name?\n”)
plyrName = input()
print (“Hello, ” + plyrName + “, would you like to add and subtract?\n”)
print (“Enter y for yes or n for no\n”)
playAns = input()
return playAns
def selectMode():
ModeNum = random.randint(0,1)
return ModeNum
def additionPractice():
tries = 1
num1 = random.randint(0,5)
num2 = random.randint(0,5)
answer = num1 + num2
while tries <= 3:
print (“What does “, num1, ” plus”, num2, “equal?\n”)
plyrAnswer = int(input())
if plyrAnswer == answer:
break
else:
print(“That isn’t correct. Please try again\n”)
tries = tries +1
if plyrAnswer == answer:
print(“Great Job!!!”)
else:
print(“The correct answer is: “, answer)
def subtractionPractice():
tries = 1
num1 = random.randint(0,5)
num2 = random.randint(0,5)
if num1 < num2:
num1, num2 = num2, num1
answer = num1 – num2
while tries <= 3:
print (“What does “, num1, ” minus”, num2, “equal?\n”)
plyrAnswer = int(input())
if plyrAnswer == answer:
break
else:
print(“That isn’t correct. Please try again\n”)
tries = tries +1
if plyrAnswer == answer:
print(“Great Job!!!”)
else:
print(“The correct answer is: “, answer)
def main():
playAns = greeting()
while playAns == “y”:
switch = selectMode()
if switch == 0:
additionPractice()
else:
subtractionPractice()
print (“Do you want to play some more?\n”)
print (“Enter y for yes or n for no”)
playAns = input()
if __name__ == ‘__main__’:
main()
## End
I would like to think that I’m getting better at this
Liam B