CS106 Summer 2021
Michael Eckmann
Lab 04
July 12, 2021
==================
Purpose:
understanding various examples of while and for loops, some with breaks
start getting experience writing functions
===================
1. Write a program named lab04prob1.py that contains a function named last_character that takes in a string as a parameter and returns the last
character in the string.
e.g.
last_character('Skidmore') # should return 'e'
last_character('Hey') # should return 'y'
===================
2.
Treat each one separately and do the following.
Read the code by eye and type up a prediction of what will print.
Run the code and see if the output agrees with your prediction.
If it does not, try to figure out why and explain why it works the way it does.
Note: if any of these are infinitely printing, then you can use Ctrl-C to kill the
program
// problem 2a.
x = 1
while x <= 5:
x = x + 1
print("The value of x is", x)
print("The final value of x is", x)
// problem 2b.
x = 1
while x > 5:
x = x + 1
print("The value of x is", x)
print("The final value of x is", x)
// problem 2c.
x = 0
while x <= 0:
x = x + 1
print("The value of x is " + str(x))
print("The final value of x is " + str(x))
// problem 2d.
x = 0
y = 5
while x < y:
x += 1
print("The value of x is " + str(x))
print("The value of y is " + str(y))
print("The final value of x is " + str(x))
print("The final value of y is " + str(y))
// problem 2e.
x = 0
while x >= 0:
x = x + 1
if x == 3:
x = -1
print("The value of x is", x)
print("The final value of x is", x)
// problem 2f.
x = 0
while x < 10:
print("The value of x is", x)
x = x + 1
print("The final value of x is", x)
// problem 2g.
x = 0
while x < 10:
x = x + 1
print("The value of x is", x)
print("The final value of x is", x)
// problem 2h.
x = 0
while x < 10:
print("The value of x is", x)
x = x + 1
print("The final value of x is", x)
===================
3.
// problem 3a.
x = 1
while x <= 5:
x += 1
if x > 3:
break
print("x is ", x)
print("The final value of x is ", x)
// problem 3b.
for x in range(100000):
if x > 3:
break
print("x is " + str(x))
print("The final value of x is " + str(x))
// problem 3c.
for x in range(100000):
if x < 3:
break
print("x is " + str(x))
print("The final value of x is " + str(x))
// problem 3d.
for x in range(10):
print("x is", x)
print("The final value of x is", x)
===================
4. Suppose the in operator doesn't exist in Python. Instead you are to write a program
named lab04prob4.py that contains a function named contains that takes in a string as
the first parameter and a character (also a string, but of len 1) and your function
should return True if the second parameter is found in the first and False if not found.
Note: you may assume the second parameter passed in is len 1
you should loop (using a while loop) through all of the indices of the first parameter
you will need to use len on the first parameter
you will need to use [ ]
e.g. contains('hey', 'e') # should return True
contains('Skidmore', 'u') # should return False
Test out your function with the following code:
word = input("Enter a word")
char = input("Enter a character")
if contains(word, char):
print(char, "is contained within", word)
else:
print(char, "is not found in", word)
Would your function work on this:
contains([1,4,3,-8], 4)
?
===================
submit:
lab04prob1.py
answers to problem 2 and 3
lab04prob4.py