CS21 Lab2: Numbers and Strings

Due Saturday (Feb. 6) before midnight

This lab assignment requires you to write several programs in Python. First, run update21. This will create the cs21/labs/02 directory and copy over any starting-point files for your programs. Next, move into your cs21/labs/02 directory and begin working on the Python programs for this lab. The pwd command helps you verify that you are in the correct sub-directory.

$ update21
$ cd cs21/labs/02
$ pwd
/home/your_user_name/cs21/labs/02
We will only grade files submitted by handin21 in this directory, so make sure your programs are in this directory!

Programming tips

As you write your first programs, start using good programming practices now:


1. Inserting repeated characters

Write a program that has the following behavior:

$ python insertChar.py

Enter some text: cs21isfun!
Enter character to insert: *
Enter number of repetitions: 3

c***s***2***1***i***s***f***u***n***!***

$ python insertChar.py

Enter some text: Swarthmore College
Enter character to insert: .
Enter number of repetitions: 2

S..w..a..r..t..h..m..o..r..e.. ..C..o..l..l..e..g..e..

2. Fun with strings

Write a program that has the following behavior:

$ python funString.py

Enter some text: ninjas

ninjas
ninja
ninj
nin
ni
n
n
ni
nin
ninj
ninja
ninjas

Be sure to test your program on other inputs.


3. Using up your points at the end of a semester

Write a program to help you figure out how to use up your leftover points at the Science Center Coffer Bar at the end of a semester. Your program should work like this:

$ python usePoints.py

Welcome to the Science Center Coffee Bar
----------------------------------------
Enter your point balance: $12.10
Enter your favorite coffee bar item: chai
Enter price of chai: $2.50
You can buy 4 chai and will have $2.10 remaining.

$ python usePoints.py

Welcome to the Science Center Coffee Bar
----------------------------------------
Enter your point balance: $13.45
Enter your favorite coffee bar item: smoothie
Enter price of smoothie: $4.00
You can buy 3 smoothie and will have $1.45 remaining.

Hint: Think carefully about which variables in your program should be integers and which should be floats.


4. Getting fit

Write a program to help users track the number of steps they walk per day. It starts by requesting a user's weekly goal, and then repeatedly asks for the daily total over the course of a week. Each day it displays the current total, steps needed to achieve the goal, and the average number of steps per day.

$ python getFit.py

Enter your weekly step goal: 35000

Day 1 steps: 4000
Steps so far:  4000   Steps remaining: 31000   Avg per day: 4000.0

Day 2 steps: 6000
Steps so far: 10000   Steps remaining: 25000   Avg per day: 5000.0

Day 3 steps: 9000
Steps so far: 19000   Steps remaining: 16000   Avg per day: 6333.3

Day 4 steps: 3500
Steps so far: 22500   Steps remaining: 12500   Avg per day: 5625.0

Day 5 steps: 10000
Steps so far: 32500   Steps remaining:  2500   Avg per day: 6500.0

Day 6 steps: 2000
Steps so far: 34500   Steps remaining:   500   Avg per day: 5750.0

Day 7 steps: 5000
Steps so far: 39500   Steps remaining: -4500   Avg per day: 5642.9

Extra Challenge

Note: extra challenges are just for fun (i.e., no bonus points). Please only attempt them after completing the regular assignment.

Add a graphical representation of the steps so far and the goal as shown in the example below.

Enter your weekly step goal: 70000


Day 1 steps: 12000
Steps so far: 12000   Steps remaining: 58000   Avg per day:12000.0
********------------------------------------------
                                             Goal|

Day 2 steps: 5000
Steps so far: 17000   Steps remaining: 53000   Avg per day: 8500.0
************--------------------------------------
                                             Goal|

Day 3 steps: 10500
Steps so far: 27500   Steps remaining: 42500   Avg per day: 9166.7
*******************-------------------------------
                                             Goal|

Day 4 steps: 20300
Steps so far: 47800   Steps remaining: 22200   Avg per day:11950.0
**********************************----------------
                                             Goal|

Day 5 steps: 10000
Steps so far: 57800   Steps remaining: 12200   Avg per day:11560.0
*****************************************---------
                                             Goal|

Day 6 steps: 10000
Steps so far: 67800   Steps remaining:  2200   Avg per day:11300.0
************************************************--
                                             Goal|

Day 7 steps: 5000
Steps so far: 72800   Steps remaining: -2800   Avg per day:10400.0
****************************************************
                                             Goal|

Submit

Remember you may run handin21 as many times as you like. Each time you run it new versions of your files will be submitted. Running handin21 after you finish a program, after any major changes are made, and at the end of the day (before you log out) is a good habit to get into.