CS21 Lab 2: Numbers, Strings, and For Loops

Due Saturday, February 4, before midnight

Goals

The goals for this lab assignment are:

  • manipulate Python numeric and string data types

  • learn to iterate over data using for loops

  • practice string operations: +, *, len()

  • practice using the range function

  • learn how to import modules that extend python’s capabilities

Work through the following sections and ask if you have questions!

Are your files in the correct place?

Make sure all programs are saved to your cs21/labs/02 directory! Files outside that directory will not be graded.

$ update21
$ cd ~/cs21/labs/02
$ pwd
/home/username/cs21/labs/02
$ ls
Questions-02.txt
(should see your program files here)

1. Math Functions

Write a program math_functions.py that practices a few built-in math functions (Reference). Note that you might have a file called math.py, but this is an accident on our part and VCode does not like the file name math.py. You can move your file to be the correct name by running the following command:

$ mv math.py math_functions.py
$ ls
Questions-02.txt  fish.py  math_functions.py  mountain.py

Your program should have the following features:

  • Import the Math functions, from math import *

  • Print the Value of pi

  • Find the highest and lowest values from (3,5,7,9) with max() and min(), and print them

  • Ask the User to enter an integer, then calculate its square root using sqrt() and print

  • Ask the User to enter an integer n, then print the pow(x, y), which is x raised to the power y, (print n lines, x = 2, y from 1 to n, using a for loop).

  • Use proper variable names

  • Use proper input data types

1.1. Sample Output

One example of running the program are shown below. User input is shown in bold.

$ python3 math_functions.py
The value of pi is 3.141592653589793
The max of (3,5,7,9) is 9
The min of (3,5,7,9) is 3
Please enter an integer to calculate square root: 3
The square root of 3 is 1.7320508075688772
Practicing pow(), please enter an integer: 5
The value of 2 raised to the power of 1 is 2
The value of 2 raised to the power of 2 is 4
The value of 2 raised to the power of 3 is 8
The value of 2 raised to the power of 4 is 16
The value of 2 raised to the power of 5 is 32

2. Fish Experiment

Write a program called fish.py that asks the user to enter an integer, as the number of weeks, then print out the number of fish remain in the fish tank each week.

Here are the details of the fish experiment:

  • At the beginning of the experiment, the researchers have eight fish, and the number of fish doubles each week.

  • Researchers remove twenty percent of the fish (round to an integer) at the end of each week.

  • After removing some fish, the researchers record the number of fish that still remain in the tank.

Your program should have the following features:

  • The user selects the number of weeks to print. You can assume the user enters a positive integer.

  • Each line should display the week number and the number of fish remaining in the tank.

  • Please review the round() function.

  • Please review the print() function, with output formatting such as %s.

2.1. Sample Output

One example of running the program is shown below. User input is shown in bold.

$ python3 fish.py
Please enter an integer, as the number of weeks you plan to observe this fish experiment: 6

Week 1, 13 fish
Week 2, 21 fish
Week 3, 34 fish
Week 4, 54 fish
Week 5, 86 fish
Week 6, 138 fish

3. Text Mountain

Write a program called mountain.py that prompts the user for a word or phrase, then prints out that phrase as a text mountain, as follows:

3.1. Text Only

First, try to write a program that just focuses on the text that needs to get printed on each line.

$ python3 mountain.py
Enter your text: cs21

c
sss
22222
1111111

$ python3 mountain.py
Enter your text: mountain

m
ooo
uuuuu
nnnnnnn
ttttttttt
aaaaaaaaaaa
iiiiiiiiiiiii
nnnnnnnnnnnnnnn

Note: You will need to figure out how many copies of a letter to print in each line.

In this 'mountain' example, your program should print out a single 'm', then three 'o’s, then five 'u’s, etc. Identify the pattern in the number of copies of each letter, then use a for loop to print out exactly the right number of each letter in each line.

Your program should have the following features:

  • The user enters a word or phrase.

  • Each printed line should consist of a single character from the string, repeated some number of times.

  • Please review the len() function.

  • Please review textbook chapter 7, strings, and focus on the string index part.

Hint:

Think carefully about the following:

  • How does the letter’s index relate to the number of characters printed on each line?

  • How does the number of levels in the mountain relate to length of the word?

length i letters

c

4

0

1

sss

4

1

3

22222

4

2

5

1111111

4

3

7

3.2. Add Spaces

Use your partial solution from the first step, and add in a number of spaces to "center" each line in the text mountain. You’ll need to know the entire string to understand how many spaces to add. In the example below, to center the mountain, the line with 'c' begins with three spaces, the line with 's' starts with two spaces, the line with '2’s starts with one additional space, and the line with '1’s starts with zero additional spaces.

$ python3 mountain.py
Enter your text: cs21

   c
  sss
 22222
1111111

$ python3 mountain.py
Enter your text: mountain

       m
      ooo
     uuuuu
    nnnnnnn
   ttttttttt
  aaaaaaaaaaa
 iiiiiiiiiiiii
nnnnnnnnnnnnnnn

4. Answer the Questionnaire

Each lab will have a short questionnaire at the end. Please edit the Questions-02.txt file in your cs21/labs/02 directory and answer the questions in that file.

Once you’re done with that, you should run handin21 again.

Submitting lab assignments

Remember to run handin21 to turn in your lab files! You may run handin21 as many times as you want. Each time it will turn in any new work. We recommend running handin21 after you complete each program or after you complete significant work on any one program.

Logging out

When you’re done working in the lab, you should log out of the computer you’re using.

First quit any applications you are running, like the browser and the terminal. Then click on the logout icon (logout icon or other logout icon) and choose "log out".

If you plan to leave the lab for just a few minutes, you do not need to log out. It is, however, a good idea to lock your machine while you are gone. You can lock your screen by clicking on the lock xlock icon. PLEASE do not leave a session locked for a long period of time. Power may go out, someone might reboot the machine, etc. You don’t want to lose any work!