The logic of an
if statement is fairly simple. If
the statement is true, the code block (indicated by lines
indented with spaces or a tab) is run. A closely relared command
if ... else, which acts precisely as you would
x = 1 and
x == 1 do completely different
=is an assignment operator: it changes to the value of
==is the comparison operator: it returns true if
xis equal to
x = 3 if x == 4: # this line will not be printed print "x is equal to 4" # one of these two lines will be printed if (x > 10) or (1 != 0): print "Expression is true" else: print "Expression is false"
The logical operators (
or) are particularly useful with
xor is useful.
() are highly encouraged when using
Generating a random number:
# extra functionality! import random # this gets a random number # between 0 and 0.999999999999 number = random.random() print "Random number is:", number
random.random()requires an extra
Create a "guessing game" program. Your program should:
intbetween 1 and 32 (inclusive).
random.random()function, but do not modify it. Instead, you should take the value it returns (a
floatbetween 0 and 0.999...) and do some math to transform that into an
intbetween 1 and 32).
intfrom the keyboard,
1if the user's guess was correct, and
0if the user's guess was wrong.
(optional: instead of guessing an
int, ask players to
float. Then, instead of trying to guess
exactly the right number, the players win if they guess a number
0.1 of the correct value.)
The simplest loops in C are
while loops. These loops
are very similar to
if statments. The difference is
that there is not
else statement included in a
while loop, and the code block will be repeated as
long as the statement is true.
Infinite loops are fun:
while True: print "Infinite loop is infinite"
Non-infinite loops are more useful:
i = 0 while i < 10: print "The loop has repeated", i, "times." i = i + 1
Save your guessing game from Exercise 5 with a new file name; we will be modifying it, but you don't want to lose all your hard work.
Modify your guessing game:
(optional: randomly change the rules -- instead of always going from 1 to 32, let the computer randomly decide to do 100 to 131, -97 to -66, etc. If you change the size of the range, you can't compare high scores for different games. However, you could program the computer to randomly select "all numbers divisible by 4 between 32 and 156" and the like.)
A loop that counts up (or down) is the most common type of loop,
so most programming languages have a short-cut: the
In python, the
for loop operates on a list of values.
We do not use the silly conditionals seen in C for loops.
The most common form of loop is this:
for i in range(10): print "The loop has repeated", i, "times."
Here's two weirder loops:
for i in range(5, 15): print "What is this loop doing? i is", i for i in range(20, 2, -2): print "What is this loop doing? i is", i
Back in the good old days, there were no fancy graphics in
computer games; we used text to represent everything. Your task
is to draw a room from a family of games called
Roguelikes -- the player (represented by the
@ symbol) must explore a dungeon.
2x2 room, player at 0,1: +--+ |..| |@.| +--+ 5x3 room, player at 1,2: +-----+ |.....| |.....| |.@...| +-----+ 14x8 room, player at 8,5: +--------------+ |..............| |..............| |..............| |..............| |..............| |........@.....| |..............| |..............| +--------------+
Your task is to write a function (with extra "helper" functions) to draw such a room.
.pyfile must contain only:
draw_room(2,2,0,1); draw_room(5,3,1,2); draw_room(14,8,8,5);Everything else must be done with functions
draw_room(), write three extra functions. The first draw a horizontal
+----+line, the second draws a line without the player
|....|, and the third draws the line with the player
draw_roomfunction should call those other helper functions when appropriate.
forloops. You may not use
whilein this exercise.
(optional: combine this exercise with keyboard input -- let the
player move around in the room, bump into walls, etc. Ask the
user to turn on the numlock key and to press
enter after every move, then you can read his moves by
reading ints from the keyboard. Use a
while loop for
Move on to Lab 3
Unless otherwise noted, all materials on these pages are licenced under a Creative Commons Licence.