CS106 Summer 2026 Michael Eckmann Lab 04 July 08, 2026 ================== Purpose: understanding how to write programmer-defined functions to achieve a task gain experience reading code with if/elif/else, bool expressions, loops and random numbers gain experience writing functions and programs that use them =================== 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. Write a program named lab04prob2.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. Note2: choose the most appropriate kind of loop and only use indicies if they are necessary 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) ? =================== 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. EXAMPLE: x = -1 y = 7 z = 8 if x < 0 and (y == 7 or z > 0): print('yes') else: print('not sure') EXAMPLE ANSWERS: # what will print if the code is executed? yes # what values of x, y and z will cause 'not sure' to print? 'not sure' will print when x >= 0 it will also print when x < 0 and y is not = 7 and z <= 0 ======= 1. a. x = 4 y = 5 z = 0 if x > 0 and y < 0: print('yes') elif x <= 0 and y < 0: print('no') elif x > 0 and y >= 0: print('maybe') else: print('not sure') # what will print if the code is executed? # what range of values of x and y will cause 'not sure' to print? ======= 1. b. z = 'Skidmore' if z > 'Hello': print('yes') elif z == 'ABC': print('no') else: print('not sure') # what will print if the code is executed? # what range of values of z will cause 'not sure' to print? ======= 1. c. x = 4 y = 5 z = 0 if x == 4 and (y == 1 or z == 0): print('yes') else: print('not sure') # what will print if the code is executed? # what values of x, y and z will cause 'not sure' to print? ======= 1. d. import random if random.random() >= 1: print('yes') else: print('no') # what will print if the code is executed? # if you ran that code say 1000 times, how many of those 1000 # times would you expect 'no' to print? ======= 1. e. import random if random.random() < 0.1: print('yes') else: print('no') # if you ran that code say 1000 times, how many of those 1000 # times would you expect 'no' to print? ======= 1. f. import random rand_num = random.randint(1,10) if rand_num < 3: print('yes') else: print('no') # if you ran that code say 1000 times, how many of those 1000 # times would you expect 'no' to print? ======= 1. g. import random rand_num = random.randint(1,10) while rand_num != 5: print(rand_num) # which of the following are true? # A. the loop will always be an infinite loop # B. the loop will always iterate exactly one time # C. the loop will always iterate zero times # D. the loop will iterate zero times or one time (depending on the value of rand_num) # E. the loop will iterate zero times or infinitely (depending on the value of rand_num) # F. the loop will iterate one time or infinitely (depending on the value of rand_num) ======= 1. h. import random rand_num = random.randint(1,10) for i in range(5): print(rand_num) # how many times will the loop iterate? # will the numbers printed always be the same, always be different, or usually be different? ======= 1. i. import random for i in range(5): rand_num = random.randint(1,10) print(rand_num) # how many times will the loop iterate? # will the numbers printed always be the same, always be different, or usually be different? =================== 4. Write a program named randomnumbers.py that fills a list with 25000 random ints between 100 and 200 inclusive on both ends, with all ints equally likely. That is, these integers should be in the interval [100, 200]. In your program you should have a programmer-defined function named create_random_list that takes in 3 parameters which are the lower limit of the range, the upper limit of the range and then how many random numbers will be generated. e.g. I should be able to call your function like this: mylist = create_random_list(100,200,25000) # mylist at this point should be a list of size 25000 containing random numbers from 100 to 200 inclusive. Write code that will print the largest, smallest and average of the numbers in the list. Verify that your list does not contain ANY integers that are < 100 or > 200, by writing code that checks each element of the list. If any do contain < 100 or > 200 print out a statement saying such. Then if those statements print out, attempt to fix your code. Similarly, if the largest actual value is not 200 and the smallest is not 100 then it is quite likely that your code for the random number is incorrect. =================== submit: lab04prob1.py lab04prob2.py answers to problem 3 randomnumbers.py