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
- Use a list comprehension to create a list of all odd numbers in the range [0,50]. Print the list to test it.
- 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.
- 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]
- 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
- 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
- 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.
-
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.
- 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
- Create a file called
numbers.txt
and write to it the first 100 numbers using a while loop
- 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
.
- Read from the previously created file to compute the average of student scores.
- Save the rainfall.txt file locally. Count how many lines are in the file.
- Write code that creates a new file called
practice.txt
and write to the string âGood morning!â
Reading from internet
- 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.
- 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
- 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â.
- 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.
- 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.
- 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.
- 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â
- 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.
- 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
- 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
- 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.