Week 1, Friday: editor, for loops


ice cream at 3pm today!


If you haven't already, please run the update21 command in a terminal window (at the unix prompt). You should get in the habit of doing this at the beginning of each class. It will copy over any files I want you to have to your cs21/inclass directory (and maybe your cs21/examples directory).

using vim

Last time we wrote a program to calculate the user's age. Let's put that program in a file now and run the file. Here's what you should have in the file (run vim howold.py to open the file, hit i for insert, and start typing):

"""
tell user about how old they are, given YOB

J. Knerr
Fall 2015
"""

def main():

  current = 2015
  userinput = raw_input("Year you were born? ")
  uyear = int(userinput)
  age = current - uyear
  print("Sometime this year you will be " + str(age) + " yrs old...")


# call the above defined function here...
main()

Things to note from the above program:

Once you have the above program typed in, run it with python howold.py and make sure it works! Test your age, a few other ages, and anything else you can think of. What happens if the user enters something silly, like "pony"? Why?

looping with for loops

We often want to do something over and over, with only a slight change in the pattern. For example, suppose we wanted to print numbers from 1 to 10? That's just "print a number" over and over, but the number changes each time (we add one to the number, then print it). Or maybe you have a list of names, and you want to send each name on the list a party invitation (i.e., for each name on the list, send them an invite). Even in a game, such as tic-tac-toe, you might be doing the same thing over and over (user turn, computer turn, user turn, ...).

Instead of writing code for each case, using a for loop is often much easier. Here is a quick example (try it in the python interactive shell!):

>>> for i in range(10):
...   print(i)
... 
0
1
2
3
4
5
6
7
8
9

The general syntax of a for loop is this:

for variable in sequence:
   do this
   and this
   and as many lines as are indented

The above loop would do those 3 lines of code with variable equal to the first item in the sequence, then again with variable equal to the second item in the sequence, and so on, for each item in the sequence.

The variable is just any valid variable name. You want to make it meaningful, but any valid name will work:

>>> for pony in range(5):
...   print(pony)
... 
0
1
2
3
4

Let's talk about the sequence. Examples of sequences (in python) are lists, strings, and files. For a list, the sequence consists of each item in the list. For a string, each character in the string. And for a file (which we won't use until week 7), each line in the file.

The range() function is just an easy way to create lists. Here are some examples:

>>> range(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> range(1,11)
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>> range(1,11,2)
[1, 3, 5, 7, 9]

So when we say for i in range(10):, the computer does this:

for i in [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]:

Which means the code block (the indented lines) will execute ten times: first with i=0, then again with i=1, and again with i=2, and so on.

Here's a simple for loop using a string sequence:

>>> for ch in "abcdefg":
...   print(ch*5)
... 
aaaaa
bbbbb
ccccc
ddddd
eeeee
fffff
ggggg

Your Turn!

See of you can write these programs:

$ python squares.py 
end number: 6
1 x 1 = 1
2 x 2 = 4
3 x 3 = 9
4 x 4 = 16
5 x 5 = 25
6 x 6 = 36

That program asks for a number, and then prints all squares from 1 to that number. The one below asks for both a character and a number.

$ python triangle.py 
char: *
 num: 5
*
**
***
****
*****