Skip to content

Instantly share code, notes, and snippets.

@zfz
Created November 29, 2012 02:35
Show Gist options
  • Save zfz/4166409 to your computer and use it in GitHub Desktop.
Save zfz/4166409 to your computer and use it in GitHub Desktop.
Mini Projets for An Introduction to Interactive Programming in Python
# Mini Project 2 for An Introduction to Interactive Programming in Python
# template for "Guess the number" mini-project
# input will come from buttons and an input field
# all output for the game will be printed in the console
# url http://www.codeskulptor.org/#user6-irmTc4hgQqb60Du.py
import random
import simplegui
# initialize global variables used in your code
n = 0
i = 0
# define event handlers for control panel
def range100():
# button that changes range to range [0,100) and restarts
global n
global i
n = random.randrange(0,100)
#print 'n=',n
i = 6
def range1000():
# button that changes range to range [0,1000) and restarts
global n
global i
n = random.randrange(0,1000)
#print 'n=',n
i = 7
def get_input(guess):
# main game logic goes here
guess = int(guess)
print 'guess =', guess
global n,i
#print 'n=',n
print 'i=',i
if i>0:
i -= 1
if guess == n:
print 'Correct!'
elif guess < n:
print 'Higher. You have guesses.', i
else:
print 'Lower. You have guesses.',
else:
print 'You have no guesses.'
# create frame
frame = simplegui.create_frame('guess', 300,200)
# register event handlers for control elements
frame.add_button('range 0-100', range100)
frame.add_button('range 0-1000', range1000)
frame.add_input('Guess', get_input, 50)
# start frame
frame.start()
# always remember to check your completed program against the grading rubric
# Mini Project 1 for An Introduction to Interactive Programming in Python
# Rock-paper-scissors-lizard-Spock template
# urL http://www.codeskulptor.org/#user6-Bb60Bm0lhE99Urb.py
import random
# The key idea of this program is to equate the strings
# "rock", "paper", "scissors", "lizard", "Spock" to numbers
# as follows:
#
# 0 - rock
# 1 - Spock
# 2 - paper
# 3 - lizard
# 4 - scissors
# helper functions
def number_to_name(number):
# fill in your code below
if number == 0:
return "rock"
elif number == 1:
return "Spock"
elif number == 2:
return "paper"
elif number == 3:
return "lizard"
elif number == 4:
return "scissors"
else :
return None
# convert number to a name using if/elif/else
# don't forget to return the result!
def name_to_number(name):
# fill in your code below
if name == "rock":
return 0
elif name == "Spock":
return 1
elif name == "paper":
return 2
elif name == "lizard":
return 3
elif name == "scissors":
return 4
else:
return None
# convert name to number using if/elif/else
# don't forget to return the result!
def rpsls(name):
# fill in your code below
print "Player Chooses " + name
# convert name to player_number using name_to_number
player_number = name_to_number(name)
# compute random guess for comp_number using random.randrange()
comp_number = random.randrange(0, 4)
print "Computer Chooses " + number_to_name(comp_number)
# compute difference of player_number and comp_number modulo five
win = (player_number - comp_number ) % 5
# use if/elif/else to determine winner
# convert comp_number to name using number_to_name
# print results
if win == 1 or win == 2:
print "Player wins!\n"
elif win == 3 or win == 4:
print "Computer wins!\n"
elif win == 0:
print "No one wins!\n"
else:
print "Error!"
# test your code
rpsls("rock")
rpsls("Spock")
rpsls("paper")
rpsls("lizard")
rpsls("scissors")
#rpsls("fuck")
# always remember to check your completed program against the grading rubric
# Mini Project 3 for An Introduction to Interactive Programming in Python
# template for "Stopwatch: The Game"
# url http://www.codeskulptor.org/#user6-9xfKUFzg7yNqyvg.py
import simplegui
# define global variables
i = 0
interval = 100
# define helper function format that converts integer
# counting tenths of seconds into formatted string A:BC.D
def format(t):
a = int(t/60000)
#print a
b = int((t - a *60000)/1000)
#print b
d = int((t- a*60000 - 1000*b)/100)
#print d
return str(a)+':'+str(b)+'.'+str(d)
# define event handlers for buttons; "Start", "Stop", "Reset"
def start():
global i
global interval
i += interval
def reset():
global i
i = 0
# define event handler for timer with 0.1 sec interval
def draw(canvas):
canvas.draw_text(format(i), [100,100],36, 'Red')
# create frame
frame = simplegui.create_frame("Stop Watch", 300, 200)
# register event handlers
frame.set_draw_handler(draw)
timer = simplegui.create_timer(interval, start)
button1 = frame.add_button("Start", timer.start, 75)
button2 = frame.add_button("Stop", timer.stop, 75)
button3 = frame.add_button("Reset", reset, 75)
# start timer and frame
frame.start()
#timer.start()
# remember to review the grading rubric
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment