CS 111 w20 lecture 6 outline
1 Poll
bet = 100 raise = input("Amount to raise: ") bet = bet + raise print("New bet:", bet)
2 float
and int
functions
- these can be used to convert text to a number
- from the example above, you could fix it by doing
raise = int(input("Amount to raise: "))
orraise = float(input("Amount to raise: "))
- difference would be in whether the user could enter a decimal number (entering a decimal number would cause an error when using
int
- difference would be in whether the user could enter a decimal number (entering a decimal number would cause an error when using
- from the example above, you could fix it by doing
3 Guessing game exercise
3.1 Write code that gets a guess from the user and prints a hint
- print "lava" if they're within 5, "warm" if they're within 20, and "cold" otherwise
- print "you got it!" if they got it
- only one hint should be printed
- start with:
import random secret = random.randint(0, 100) print("guess a number between 0 and 100")
4 Repeated guesses
- how would we give the user 5 guesses?
- could copy paste the code, printing out a different number of guesses left each time
- potential problems with this approach?
- fortunately, Python gives us a way to manipulate the instruction pointer to repeat a section of code
4.1 The for
loop
for loop_variable in SEQUENCE: # repeated code goes here # more repeated code # this code is not repeated
See guess.py
for full example
5 Loop mystery
1: x = 0 2: for i in range(2, 6, 1): 3: x = x + i 4: print(x)
- line trace: 1, 2, 3, 2, 3, 2, 3, 2, 3, 4
5.1 Unrolled
x = 0 i = 2 x = x + i i = 3 x = x + i i = 4 x = x + i i = 5 x = x + i print(x)
6 Graphics example
Computers aren't just for helping us with mechanical or informational tasks, they can be aesthetic or creative tools as well. The code below is linked from the web page as circles.py
.
# for loop example using Portable Graphics Library # requires pgl.py # CS 111, Aaron Bauer from pgl import GWindow, GOval # import what we need from Portable Graphics Library import random from random_color import random_hex_color # these are constants, values set at the start of the program that should never change # have all caps names by convention GWINDOW_WIDTH = 500 GWINDOW_HEIGHT = 800 # first thing is to create the graphics window (GWindow) that will # display our graphics, and set its width and height gw = GWindow(GWINDOW_WIDTH, GWINDOW_HEIGHT) circle = GOval(250, 250, 100, 100) gw.add(circle) # you would think that position (250, 250) would be the center of the window # so why does the circle appear off center? # (250, 250) is the center, but the position we give the circle is the location of its UPPER-LEFT CORNER count = int(input("How many circles would you like? ")) # repeat the following code count number of times for i in range(count): # create a circle at a random position # the x and y position we give the circle is the location of its upper-left corner # to make sure it stays within the window, limit x to 490 (window dimension of 500 - circle dimension of 10) # same idea for y circle = GOval(random.randint(0, 490), random.randint(0, 790), 10, 10) circle.setFilled(True) # make the circle filled in with color circle.setFillColor(random_hex_color()) # set the color filling in the circle randomly gw.add(circle) # the circle won't appear unless we add it to the GWindow
7 Exercise: step 2 from lab 2
You are given GWINDOW_WIDTH
, GWINDOW_HEIGHT
, BALL_SIZE
, use these to create a solid black circle at the center of the window
8 Pair programming
Read the guidelines closely!