CS21 Lab 3: A perfect leet adventure

Due 11:59pm Tuesday, 21 September

Run update21, if you haven't already, to create the 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.

1. Choose your own adventure game

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.



2. P42d0~ m3, d0 y0u 5p34k 1337?

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!



3. Perfect numbers

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.

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 or imperfect. 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 imperfect

$ 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

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?

3b. A bonus problem: seeking perfection

This problem is an optional bonus problem, is not required, and you should not attempt it until you are completely finished with the other three lab problems.

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


Submit
Once you are satisfied with your programs, hand them in by typing handin21 at the unix prompt. You may run handin21 as many times as you like, and only the most recent submission will be recorded. This is useful if you realize after handing in some programs that you'd like to make a few more changes to them.