class logo

CS 122 - Day 7 Exercises

This page lists some problems for today's class. After today, you should be able to solve these problems on your own. Problems with this color are more challenging.

1) Guessing Game

Create a variable with an integer value. Ask the user to guess the value, check their guess against the number and print "correct" if they guessed the value correctly.

Enhancement 1 Add an else clause to your if statement to print "incorrect" if the user's guess is wrong.

Enhancement 2 Use elif clauses and > and < to check whether the guess is too high or too low and report that to the user.

Enhancement 3 Put the entire thing in a while loop so that the user must keep guessing until they either get it correct or type a 'q' to quit.

Enhancement 4 Use the random library to choose a random secret number for the user to guess. You must first import the random library and then use it to assign a value to the variable. The code to get a random integer between 1 and 100 looks something like this:

import random secretNum = random.randint(1, 100)

Here's my solution.

2) Lists

Create a list and add 7 random numbers to it. Then, using the solutions I provided you in our exercises last week, find the maximum value in the list.

Enhancement 1 Reverse the list into another variable called "reversed".

Enhancement 2 Sort the list into another variable called sorted.

Enhancement 3 Use the built in functions reverse() and sort() to sort your original list.

Here's my solution.

3) Math quiz

This program should test the users addition skills. Use the random library to generate two random numbers, then ask the user to add them up. Check to see if the user's answer is correct.

Enhancement 1 Put the above code inside a loop so that the user is asked a series of questions. Keep track of how many they get correct and display their success rate at the end of the quiz.

Enhancement 2 Add subtraction problems as well. Use the random number generator to randomly choose whether to show an addition problem or a subtraction problem. The easiest way to do this is to generate a random number that is either 0 or 1, then if it is zero, do addition and if it is 1 do subtraction.

Enhancement 3 Use the built in functions reverse() and sort() to sort your original list.

Here's my solution.

4) File I/O

Imagine that you are running an experiment using some computerized monitoring tool, and as an output from the experiment you get a file containing a bunch of readings. You need to examine the data to look for certain attributes. You could import it into a spreadsheet which would let you find simple things like the minimum, maximum, and average values; but what if you needed more complicated information, like are there any runs of 5 increasing values? Problems like these can be easily solved by writing a script, but can be a pain to do by hand.

Download the data file data.txt, write a python program to read it and find the following values. The data file is just made up of numbers, one number per line.

  • Maximum value
  • Minimum value
  • Average value, the sum of all the values divided by the number of values

My solution

Enhancement 1 Record the line numbers where the minimum and maximum values were observed. If the minimum or maximum value occurs more than one time in the file, record all the line numbers on which it occurs.

Enhancement 2 Look for a number of consecutive increasing or decreasing numbers and report if it exists in the data. Ask the user for the number of consecutive readings to look for.

Enhancement 3 Write the findings to a file called "summary.txt".