Run update21, if you haven't already, to create cs21/labs/03. Then cd into your cs21/labs/03 directory and create the python programs for lab 3 in this directory (handin21 looks for your lab 3 assignments in your cs21/labs/03 directory):
$ update21 $ cd $ cd cs21/labs/03 $ pwd /home/your_user_name/cs21/labs/03
Your programs are graded on both correctness and style. Please review
the comments regarding programming style on the main page.
In this problem you will create a simple text-based adventure game! Write a program in the file named adventure.py that allows the player to select from some set of actions, and given their selection, allows them to explore some world. For instance, one possible solution might be:
$ python adventure.py You find yourself in a dark room. You look around and see a light switch and a dragon. What do you want to do? (1) flip the light switch (2) run away (3) poke the dragon Type the number of your action: 3 You poked the dragon. This allows you to better see the room, but seems to have set your hair on fire. What do you want to do? (1) sit at the computer in front of you and finish your CS homework (2) try to put out the fire using a bowl of whipped cream from Sharples (3) nothing Type the number of your action: 1 You sat at the computer and worked on your CS homework until you died from severe scalp burns. You lose. $ python adventure.py You find yourself in a dark room. You look around and see a light switch and a dragon. What do you want to do? (1) flip the light switch (2) run away (3) poke the dragon Type the number of your action: 19 19 is not a valid choice. You lose.
Your game should include at least three choices for the player's first action, and at least one decision should allow the player to select from another set of actions before the game ends. If a player enters an invalid number that does not correspond to an action, print an appropriate error message and end the game.
Elite computer hackerz like to be quirky and replace some of their letters with numbers or symbols when they type; this is called leet speak. Write a program, in the file leet.py, that takes a string as input, converts that string to leet speak, and prints it.
To convert a string to leet, change all 'e' and 'E' to the number 3, change all 'l' and 'L' to the number 1, and change all 's' and 'S' to the number 5. For example:
$ python leet.py Enter a string to convert: We love Computer Science W3 1ov3 Comput3r 5ci3nc3 $ python leet.py Enter a string to convert: Swarthmore College 5warthmor3 Co113g3 $ python leet.py Enter a string to convert: What fun! What fun!
A perfect number is an integer that is equal to the sum of its divisors (except itself). For instance, 6 is a perfect number because its divisors are 1, 2, 3, and 6, and 1+2+3 is equal to 6. 10 is not a perfect number because its divisors are 1,2,5, and 10, and 1+2+5 is not equal to 10. If the sum of the divisors is less than the integer itself, the integer is 'deficient'. If the sum is greater than the integer, it's 'abundant'.
Write a program, in the file perfect.py that takes as input an integer, prints all divisors of that integer (except for the integer itself), computes the sum of those divisors, and prints whether that integer is perfect, deficient, or abundant. For example:
$ python perfect.py This program checks if an integer is perfect. Enter an integer: 10 1 is a divisor 2 is a divisor 5 is a divisor The sum of the divisors is 8 10 is deficient $ python perfect.py This program checks if an integer is perfect. Enter an integer: 28 1 is a divisor 2 is a divisor 4 is a divisor 7 is a divisor 14 is a divisor The sum of the divisors is 28 28 is perfect $ python perfect.py This program checks if an integer is perfect. Enter an integer: 40 1 is a divisor 2 is a divisor 4 is a divisor 5 is a divisor 8 is a divisor 10 is a divisor 20 is a divisor The sum of the divisors is 50 40 is abundant
Hint: you can easily check to see if a number x is a divisor of y by using the % remainder function. If x is a divisor of y, what is y % x?
Hint #2: it is a good idea to develop code in stages, testing
as you go. For this problem, you might start by finding and printing
all divisors. Then, once you have that working, figure out a way
to sum all of the divisors.
This problem is an optional bonus problem. It is not required, and you should not attempt it until you are completely finished with the other three lab problems. Bonus problems in this course earn extremely few extra points; your reward is mostly the extra knowledge and satisfaction you get from solving the problem.
In the file findAllPerfectNumbers.py, write a program that takes an integer as input and prints all perfect numbers up to (and including) that integer. For example:
$ python findAllPerfectNumbers.py This program finds all perfect numbers up to some max integer. Enter the max integer to search: 50 6 is perfect 28 is perfect