Activity: What else? 🤔

In addition to the earlier activities and assignments, below are a set of example problems to help you prepare for the exam. Pick any FIVE problems that look the most challenging, and complete them. It will serve you best to pick the ones that you need the most practice with because any of these questions might be on the exam.

List comprehensions

  1. Use a list comprehension to create a list of all odd numbers in the range [0,50]. Print the list to test it.
  2. Use a list comprehension to create a list of all numbers divisible by 4 within the range [0,100], that is, the list 0,4,8,..,100. Print the list to test.
  3. Use a list comprehension to create a list of size 10 with alternating True/False values, beginning with True. Print the list to test it. It should appear as [True,False,True,False,True,False,True,False,True,False]
  4. Use a list comprehension to create a list of the squares of the first 20 positive integers, that is, the list 1,4,9,25,36, etc. Print the list to test it.

While loops

  1. Write code that sums up all the positive numbers entered by the user. When the user enters a negative number, the program displays the sum of the positive numbers and how many numbers were entered. Hint: Use a while loop to check if the number is positive or negative. Note that the negative number doesn’t contribute to the sum. Next, compute the mean of the positive numbers. Do not use the keyword break. An example execution is below:
    Enter next number (negative number to stop): 5
    Enter next number (negative number to stop): 10
    Enter next number (negative number to stop): 3
    Enter next number (negative number to stop): -1
    Sum of all positive numbers:  18
    
  2. Create a game 🎲 in which the user tries to guess a number matching their (artificial) opponent’s. The game will repeatedly prompt the user for a number between 1 and 10 (or 0 to quit). Store the user input result in an integer variable guess. Use the function random.randint(1,10) to generate a random number between 1 and 10. Store the result in another variable answer - this is the opponent’s number. Print out whether the user’s guess matches the correct answer (i.e., if the user won or the computer won). Keep track of the number of instances that the computer or user won. When the user enters 0 to quit, print out the total. For example:
     >>> Enter your guess between 1-10 [0 to quit]:  2
     Nope, the correct answer was 5! Computer wins
     Enter your guess between 1-10 [0 to quit]:  8
     Nope, the correct answer was 5! Computer win
     Enter your guess between 1-10 [0 to quit]:  3
     You win!
     Enter your guess between 1-10 [0 to quit]:  0
    
     Thanks for playing. You won 1 out of 3 games.
    
  3. Write code that will keep asking the user to enter in a number. Once the user enters in a negative number, it should terminate and print the sum of all positive numbers entered. For example, if the user enters 1, 2, 3, and -1, then the program will output 6. Do not use the keyword break.

  4. Write code that will keep asking the user to enter in a username, then a password. Once the user enters in ‘q’, it should terminate and print a dictionary containing all usersnames and passwords.

Reading/writing to files

  1. Create a file called numbers.txt and write to it the first 100 numbers using a while loop
  2. Create a file called scores.txt. Use a while loop to prompt the user to type in student names and corresponding scores to write to the file. End the program when the user types q.
  3. Read from the previously created file to compute the average of student scores.
  4. Save the rainfall.txt file locally. Count how many lines are in the file.
  5. Write code that creates a new file called practice.txt and write to the string “Good morning!”

Reading from internet

  1. Read (but do not download) the file from http://csweb.wooster.edu/hguarnera/cs100/assignments/rainfall.txt. Output the contents of the page to the terminal decoded in UTF-8 format.
  2. Read (but do not download) the file from https://csweb.wooster.edu/hguarnera/cs100/assignments/loremipsum.txt, decoded in UTF-8 format. Count the number of words in the file and print it out.

Image manipulation

  1. Write code that creates a new 300 by 300 pixel image with white pixels and a 100 by 100 pixel blue square in the center. Dsiplay it on a window, then save the image to a file called “img.gif”.
  2. Write code that creates a new 100 by 100 empty image. In the center of the screen, add a horizontal line that spans the entire window. Display the image on a window to test it.
  3. Write code that creates a new 100 by 100 image, where each pixel is a randomly generated color. Display the image on a window to test it.
  4. Write code that creates a new 100 by 100 empty image. In the center of the screen, add a horizontal line that spans the entire window. Display the image on a window to test it.
  5. Download the following image locally: lutherbell.gif. Write code that negates the image, and display it on a window to test it. In order to negate an image, iterate over each pixel and modify it. For example, given a pixel with old values R’, G’, and B’, the negated pixel will have values:
    • R = 255 - R’
    • G = 255 - G’
    • B = 255 - B’
  6. Download the following image locally: kauke.jpg. Write code that converts the image to gray scale, and display it on a window to test it. To convert an image to gray scale, iterate over each pixel and modify it. For example, take a pixel with old values R’, G’, and B’. To create a gray-scale pixel, set each R, G, and B value to be the average of R’, G’, and B’. For example, a pixel with RGB value (0, 100, 155) will have RGB value (85, 85, 85) in gray-scale.
  7. Challenging: Download the following image locally: kauke.jpg. Write code that renders a single modified image to the window. The top left quarter of the image is normal, the top right is negated (use formula above), the bottom left quarter is in gray scale (use formula above), and the bottom right quarter is normal.

Classes

  1. Write code to create a Dog class. Define its constructor to accept arguments for the following: name (a string representing the name of the pet dog) and age (an integer representing the age in dog years), which are saved as attributes. Write a function in the class called get_human_age which computes the age of the dog in human years, which is 7 times the age in dog years. Test your code using the following:
    d = Dog("Bandit", 2)
    print( d.get_human_age() ) # prints 14
    
  2. Write code to create a Holiday class. Define its constructor to accept the name of the holiday and save it to an attribute. Test your code by creating a holiday:
    h = Holiday("Thanksgiving")
    

How to submit

Submit your working python file to Moodle. Please include any input files that your program needed.