CS21 Lab 3: Loops and Conditionals

Due Saturday, February 11, by 11:59pm

Programming Tips

As you write programs, use good programming practices:

  • Use a comment at the top of the file to describe the purpose of the program (see example).

  • All programs should have a main() function (see example).

  • Use variable names that describe the contents of the variables.

  • Write your programs incrementally and test them as you go. This is really crucial to success: don’t write lots of code and then test it all at once! Write a little code, make sure it works, then add some more and test it again.

  • Don’t assume that if your program passes the sample tests we provide that it is completely correct. Come up with your own test cases and verify that the program is producing the right output on them.

  • Avoid writing any lines of code that exceed 80 columns.

    • Always work in a terminal window that is 80 characters wide (resize it to be this wide)

    • In vscode, at the bottom right in the window, there is an indication of both the line and the column of the cursor.

Function Comments

All functions should have a top-level comment! Please see our function example page if you are confused about writing function comments.

Are your files in the correct place?

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

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

Goals

The goals for this lab assignment are:

  • practice using if-else statements

  • continue working with for loops and accumulators

  • practice using bool Boolean type and logical operators

  • use formatted printing

1. Vowels and disemvoweling

Write a program vowels.py that prompts the user for a string and then reports how many vowels it contains. Then the program should print the "disemvoweled" version of the string, which is the original string with all vowels removed.

Here are some examples of how the program should work:

$ python3 vowels.py
Enter a string: Hello, world!
Contains 3 vowels
Disemvoweled: Hll, wrld!
$ python3 vowels.py
Enter a string: SWARTHMORE COLLEGE
Contains 6 vowels
Disemvoweled: SWRTHMR CLLG
$ python3 vowels.py
Enter a string: A billion is either 1,000,000,000 or 1,000,000,000,000, depending which number system you use.
Contains 20 vowels
Disemvoweled:  blln s thr 1,000,000,000 r 1,000,000,000,000, dpndng
whch nmbr systm y s.
$ python3 vowels.py
Enter a string: The quick onyx goblin jumps over the lazy dwarf.
Contains 12 vowels
Disemvoweled: Th qck nyx gbln jmps vr th lzy dwrf.

In these examples, the letter 'y' has not been considered a vowel.

Note that you need to check for and remove both upper- and lower-case vowels.

2. Looking for prime numbers

Prime numbers are integers > 1 which are only divisible by 1 and themselves. For example, 17 is a prime number because it can only be divided by 17 and 1, but 24 is not a prime number because it can be divided by other numbers (2x12, 3x8, 4x6, and 1x24).

Write a program prime.py that asks a user for a number, and determines if that number is a prime. Your program should loop through the numbers from 1, 2, …​, up to the square root of the user-input number, and check whether the user-input number can be evenly divided.

If the number is a prime, your program should announce that. If the number is not a prime, your program should print out all the different ways it can be factored.

Here are some examples of how your program should work.

$ python3 prime.py
Enter a number: 24
1 x 24 = 24
2 x 12 = 24
3 x 8 = 24
4 x 6 = 24
Sorry, 24 is not a prime number.
$ python3 prime.py
Enter a number: 17
1 x 17 = 17
Congratulations, 17 is a prime number!
$ python3 prime.py
Enter a number: 32
1 x 32 = 32
2 x 16 = 32
4 x 8 = 32
Sorry, 24 is not a prime number.
$ python3 prime.py
Enter a number: 59
1 x 59 = 59
Congratulations, 59 is a prime number!

Notice that the program does not repeat vice-versa factorizations (so it prints "2 x 12 = 24" but doesn’t also print "12 x 2 = 24"). When you look for factors, you only need to check up to the square root of the original number. Anything beyond that point will only repeat already-discovered factors.

Python has a built-in sqrt() function in the math library which can be used to calculate the square root of a number. Remember that in order to use the math library, you will need to use an import statement at the top of your program.

Hint: There are many ways to determine if one number evenly divides another, and you should plan your approach before you start to write code. You can use python’s mod operator (%) to determine whether one number is evenly divisible by another --- you should experiment with this operator to understand how it works!

3. Holiday decorations

Some people love to decorate for the holidays! Write a program, holiday-decorations.py, that requests the current month and date and determines which holiday decorations should be displayed on that day. Holiday decorations are usually put up leading up to the holiday, but as soon as the holiday passes, it’s time to switch to the decorations for the next upcoming holiday!

Here are the holiday decoration schedules for 2023:

start end holiday

Jan 1

Feb 2

Groundhog Day

Feb 3

June 2

National Donut Day

June 3

June 30

Asteroid Day

July 1 (or June 31)

September 13

World Chocolate Day

September 14

October 31

Halloween

November 1

December 31

New Year’s

Here are some examples to show how your program should work:

$ python3 holiday-decorations.py
Enter current month (1-12): 10
Enter current day: 5
Today is 10/5, and you should be using the decorations for Halloween right now!
$ python3 holiday-decorations.py
Enter current month (1-12): 2
Enter current day: 14
Today is 2/14, and you should be using the decorations for National Donut Day right now!
$ python3 holiday-decorations.py
Enter current month (1-12): 6
Enter current day: 30
Today is 6/30, and you should be using the decorations for Asteroid Day right now!
$ python3 holiday-decorations.py
Enter current month (1-12): 7
Enter current day: 1
Today is 7/1, and you should be using the decorations for World Chocolate Day right now!

If the user enters an invalid month or day, then the program lets the user know it was an invalid choice and picks a default month (January) and day (1st) instead, then gives the corresponding output even though it wasn’t what the user requested. (You may assume that every month has 31 days, so any day numbered 1 through 31 is valid. This includes days like February 30 and September 31.) For example:

$ python3 holiday-decorations.py
Enter current month (1-12): 300
Enter current day: 15
Invalid month, assuming January.
Today is 1/15, and you should be using the decorations for Groundhog Day right now!
$ python3 holiday-decorations.py
Enter current month (1-12): 5
Enter current day: -43
Invalid day, assuming the 1st.
Today is 5/1, and you should be using the decorations for National Donut Day right now!
$ python3 holiday-decorations.py
Enter current month (1-12): 11
Enter current day: 31
Today is 11/31, and you should be using the decorations for New Year's right now!

Note that June 31, although it isn’t a real day, is a possible day that the program will need to work with. Since it occurs "after" Asteroid Day, but before the July 1 start of preparation for World Chocolate Day, you can choose either as a possible behavior for your program.

One way to handle June 31 is this:

$ python3 holiday-decorations.py
Enter current month (1-12): 6
Enter current day: 31
Today is 6/31, and you should be using the decorations for World Chocolate Day right now!

Another way to handle June 31 is this:

$ python3 holiday-decorations.py
Enter current month (1-12): 6
Enter current day: 31
Today is 6/31, and you should be using the decorations for Asteroid Day right now!

3.1. Extra holiday directions (OPTIONAL)

It can be helpful to receive more specific directions, so we’d like to add a feature to give detailed directions depending on how close to the holiday we are.

If you have time and interest, copy your completed holiday-directions.py into a new file `holiday-directions-fancy.py':

cp holiday-directions.py holiday-directions-fancy.py

In the fancy version of the program, add extra details so that if the holiday is less than a week away, the message is extra-excited, and if the holiday just passed yesterday, then the user gets told to take down the old decorations.

$ python3 holiday-decorations-fancy.py
Enter current month (1-12): 9
Enter current day: 8
Today is 9/8, World Chocolate Day is right around the corner!
$ python3 holiday-decorations-fancy.py
Enter current month (1-12): 3
Enter current day: 15
Today is 3/15, and you should be using the decorations for National Donut Day right now!
$ python3 holiday-decorations-fancy.py
Enter current month (1-12): 9
Enter current day: 14
Today is 4/23, so you should be taking down the decorations for World Chocolate Day and putting up the decorations for Halloween!

Note that if the current date is more than a week before a holiday and is not the day ofter the last holiday, then the fancy program should do the same thing as the standard program.

4. Answer the Questionnaire

Each lab will have a short questionnaire at the end. Please edit the Questions-03.txt file in your cs21/labs/03 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!