CS1 - 1011 - February

From WLCS

Friday-Monday (2/25/11-2/28/11)

Warmup:

  • Copy the following code into python, which is supposed to print every character of s
s="Puff Dragon"
i=1
while i<len(s)-1:
	print s[i]
	i+=1
  • Fix the code
  • Change the code so it prints in reverse order, from end to start.

Agenda:

Wednesday - Thursday (2/24/11)

Warmup:

  • Copy the following code into python
def matches(target,test):
    len_target=len(target)
    if test[0:len_target]==target:
        return True
    else:
        return False
        
target=raw_input("Enter target string:")
test=raw_input("Enter test string:")
print matches(target, test)
  • Try it with each of the following input pairs:
target test result
out outside
inner in
ann Ann
side outside
mi michael
  • What overall rule does the program follow - be specific?
    • Try your own inputs, and see if the results match your expectation.
    • Why might this be useful?
  • Add comments to the code, describing what each line does - be specific.

Agenda:

Friday - Tuesday (2/18/11-2/22/11)

Warmup:

  • Jot notes on the format of an email address:
    • What character must be in an email?
    • What characters can never be in an email?
    • Within a sentence, how are words separated?
    • How could you detect where an email starts and end?

Agenda:

Wednesday - Thursday (2/16/11-2/17/11)

Warmup:

  • Using python, create a variable sample and set it to "We're Building a Better Internet".
  • Write expressions that:
    • Print the first character
    • Print the third character (the apostrophe)
    • Print the first "B"
    • Print the second from last character (hint: use the len() function)
    • Print the word 'Building'
    • Print the word 'Internet'
  • Tip: Use print ":"+sample[x:y]+":" to see exactly what is in your slice (check for extra spaces at the start or end).

Agenda:

Monday - Tuesday (2/14/11-2/15/11)

Warmup: When is 1 + 1=11?

  • Write a short program that asks the user for two values using raw_input (not input).
  • Add the two values and store them in a third variable, then print this variable.
  • Test your program by entering your first name and last name.
  • Try it again with the values ‘1’ and ‘1’

Agenda:

Friday (2/11/11)

Warmup:

  • Review the Robot project rubric - work out how you can get most points
  1. Player sprite must be able to move in four directions using the keyboard : 5 pts
  2. The player sprite wraps at the edges of the screen : 10 pts
  3. 5-10 collectable items (coins) are created and placed onscreen: 10 pts
  4. Items are removed once the player ‘collects’ them: 10 pts
  5. A robot sprite follows the player around the screen : 10 pts
  6. If the player collides with the robot, then the player loses (and the game ends)  : 5 pts
  7. If the player collects all the items, then the player wins (and the game ends): 10 pts
  8. Code legibility (meaningful variable names and code comments): 10 pts
  9. Code parsimony (no redundant or convoluted code): 10 pts
  10. Completed on time: 10 pts

Agenda:

  • Demo Robot game  : due today
    • Then put your name at the top, print it and submit to schoolweblockers with your name in the filename.
  • If you've completed Robot, work on the Dodge game
  • Depending upon how many people are complete, we may start on String- see File:Strings Python.ppt

Wednesday-Thursday (2/9/11-2/10/11)

Warmup:

  • Variable scope
  • Without using a computer write down answers to the following questions:
  1. Where is 'myVar' defined (created)?
  2. In what order will the print statements be run?
  3. What value will 'myVar' have each time?
    Now copy the program into Python and test it.
  4. What values were actually printed?
  5. Was this the same or different from what you expected? Why so? Discuss with your partner.
  6. Inserted the following line at line 5 (just after the 'def test' line)
    print "In test, at start, myVar=", myVar
    Why does this create an error?
myVar=0
print "At start, myVar=",myVar

def test(input_var):
    myVar=input_var+1
    print "In test, at end, myVar=",myVar
    
myVar=1
print "Before test, myVar=:",myVar

test(myVar)
print "At end, myVar=:",myVar

Agenda:

  • Demo Robot game  : due for P2 class on Friday (2/11/11) or Monday for P1 class (2/14/11)
    • Then put your name at the top & print it
    • Finally, rename it LASTNAME_FIRSTNAME_Robot.py and submit on school web lockers
  • If you've completed Robot, work on the Dodge game
  • We'll start on Strings (HTTLCS Ch 7) next week

Monday-Tuesday (2/7/11-2/8/11)

Warmup:

  • Replacing constants with variables
In this example, boxes are created in the four corners and center of the window.
But what if we changed the size of the window (line 2)? Each box would have to be changed too - not very efficient.
  • Instead, create variables for the window width (400) and height (300), and the box size (20).
Then replace the box's coordinates (lines 4-8) with a 'variable expression'
So line 4 would be Box((windowWidth-boxSize,0),boxSize,boxSize,filled=False,color=color.RED,thickness=10)
  • Test your code by changing the variable values, and checking the boxes are still in the corners/center.
from gasp import *
begin_graphics(400, 300, title="Four corners", background=color.ORANGE)

Box((0,0),20,20,filled=False,color=color.RED,thickness=10)  #At bottom left corner
Box((380,0),20,20,filled=False,color=color.RED,thickness=10) #At bottom right corner
Box((0,280),20,20,filled=False,color=color.RED,thickness=10) #At top left corner
Box((380,280),20,20,filled=False,color=color.RED,thickness=10) #At top right corner
Box((190,140),20,20,filled=False,color=color.RED,thickness=10) #At center
sleep(6)
end_graphics()

Agenda:

  • Continue work on Robot game  : due Thursday (2/10/11)
    • Submit your code to school web lockers when you're done (semester 2) - please put your name at the top of your code.
  • Small group review of the Debugging short exercise
  • If you've complete & submitted Robot, review HTTLACS: Ch7 on strings. There are exercises to complete.

Friday (2/4/11)

Warmup:

Agenda:

Wednesday - Thursday (2/2/11-2/3/11)

Warmup:

  • The following code is supposed to wrap the player in Robot.
from gasp import *
begin_graphics(800, 600, title="Catch", background=color.YELLOW)
ball_x=-10
ball_y = 300
ball = Circle((ball_x, ball_y), 10, filled=True)
set_speed(120)

while True:
    if ball_x<=0:   # REPLACE WITH COMMENT 
         ball_x=800
    elif ball_x>=800:  # REPLACE WITH COMMENT 
         ball_x=0
    move_to(ball,( ball_x,ball_y))
    update_when('next_tick')

end_graphics()
  • Copy and paste this code, and save it as a new program, and test it
  • Next, replace the comments in the code with a description of what that section does
  • Finally, write answers to the following questions, then discuss them with your partner:
  1. What behavior do you see?
  2. Why do you think this is happening (what's your hypothesis)?
  3. How could you test your hypothesis?
  4. How can you fix the code?

Agenda:

Tuesday (2/1/11)

Warmup:

  • Write a program that prompts the user for x and y coordinates between 0 and 200. Compare their input to a point at (75, 100). Tell the user if they are left or right of that point, and by how much. Likewise, tell them if they are below or above that point , and by how much.

Agenda: