Assignment #5 (Loop)
Assignment #5 (Loop)
To Programming (Python)
Assignment No. 5: Loop
Exercise 1 – Understanding loops
For each of the following fragments of code, write what the output would be. Again, do this without running
the code (although feel free to check yourself when you’re done).
1. num = 10
while num > 3:
print num
num = num - 1
2. divisor = 2
for i in range(0, 10, 2):
print (i/divisor)
3. num = 10
while True:
if num < 7:
break
print num
num -= 1
4. count = 0
for letter in ’Snow!’:
print ’Letter #’, count, ’is’, letter
count += 1
n = 10
i = 10
while i > 0:
print i
if i % 2 == 0:
i=i/2
else:
i=i+1
What do you think this code is doing? Without comments it is hard to guess what Ben’s intention was
(*cough* this is why the staff loves to look at commented code!!), so read through it and make a sensible
guess as to what it is doing. There’s a lot of mistakes in the code so your guess is as good as ours!
1. Draw a table that shows the value of the variables n and i during the execution of the program. Your
table should contain two columns (one for each variable) and one row for each iteration. For each row
in the table, write down the values of the variables as they would be at the line containing the print
statement.
2. Ben made a lot of mistakes. State what you think Ben was trying to do and suggest one or more ways
he could fix his code (there’s a few good answers for this depending on what you think the code should
be doing).
1. Using a for loop, write a program that prints out the decimal equivalents of 1/2, 1/3, 1/4,..., 1/10.
2. Write a program using a while loop that asks the user for a number, and prints a countdown from that
number to zero. What should your program do if the user inputs a negative number? As a
programmer, you should always consider “edge conditions” like these when you program! (Another
way to put it- always assume the users of your program will be trying to find a way to break it! If you
don’t include a condition that catches negative numbers, what will your program do?)
3. Write a program using a for loop that calculates exponentials. Your program should ask the user for a
base base and an exponent exp, and calculate baseexp.
4. Write a program using a while loop that asks the user to enter a number that is divisible by 2. Give the
user a witty message if they enter something that is not divisible by 2- and make them enter a new
number. Don’t let them stop until they enter an even number! Print a congratulatory message when
they *finally* get it right.
When you are done, print a copy of the file and turn it in. Make sure your name and section number is in the
comment section of your program.
A = the month of the year, with March having the value 1, April the value 2, . . ., December the value 10, and
January and February being counted as months 11 and 12 of the preceding year (in which case,subtract 1 from
C)
B = the day of the month (1, 2, 3, . . . , 30, 31)
C = the year of the century (e.g. C = 89 for the year 1989)
D = the century (e.g. D = 19 for the year 1989)
Note: if the month is January or February, then the preceding year is used for computation. This is because
there was a period in history when March 1st, not January 1st, was the beginning of the year.
Let W, X, Y, Z, R also denote integer variables. Compute their values in the following order using integer
arithmetic:
W = (13*A - 1) / 5
X =C/4
Y =D/4
Z = W + X + Y + B + C - 2*D
R = the remainder when Z is divided by 7
The value of R is the day of the week, where 0 represents Sunday, 1 is Monday, . . ., 6 is Saturday. If the
computed value of R is a negative number, add 7 to get a non negative number between 0 and 6 (you don’t
need to do this in the code). Print out R. You can check to be sure your code is working by looking at
http://www.timeanddate.com/calendar/.
Run some test cases- try today’s date, your birth date, and whatever else interests you!
Feel free to submit your zellers.py code if you wish, we’ll take a look at it if you do.
Plain: ABCDEFGHIJKLMNOPQRSTUVWXYZ
Cipher: DEFGHIJKLMNOPQRSTUVWXYZABC
To be able to generate the cipher above, we need to understand a little bit about how text is represented
inside the computer. Each character has a numerical value and one of the standard encodings is ASCII
(American Standard Code for Information Interchange). It is a mapping between the numerical value and the
character graphic. For example, the ASCII value of ’A’ is 65 and the ASCII value of ’a’ is 97. To convert
between the ASCII code and the character value in Python, you can use the following code:
letter = ’a’
Start small. Do not try to implement the entire program at once. Break the program into parts as follows:
1. Create a file called cipher.py. Start your program by asking the user for a phrase to encode and the shift
value. Then begin the structure of your program by entering in this loop (we’ll build on it more in a
bit):
encoded_phrase = ’’
What does this loop do? Make sure you understand what the code does before moving on!
2. Now modify the program above to replace all the alphabetic characters with ’x’. For example:
We are going to apply the cipher only to the alphabetic characters and we will ignore the others.
3. Now modify your code, so that it produces the encoded string using the cyclic cipher with the shift
value entered by the user. Let’s see how one might do a cyclic shift. Let’s say we have the sequence:
012345
If we use a shift value of 4 and just shift all the numbers, the result will be:
456789
We want the values of the numbers to remain between 0 and 5. To do this we will use the modulus
operator. The expression x%y will return a number in the range 0 to y-1 inclusive, e.g. 4%6 = 4, 6%6 =
0, 7%6 =1. Thus the result of the operation will be:
450123
Hint: Note that the ASCII value of ’A’ is 65 and ’a’ is 97, not 0. So you will have to think how to use the
modulus operator to achieve the desired result. Apply the cipher separately to the upper and lower
case letters.
Here is what you program should output:
When you are done, print a copy of the file and turn it in. Make sure your name and section number is
in the comment section of your program.