CS21 Lab 3: Booleans and Decision Structures

Due Saturday, September 30, before midnight

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

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

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.

Optional warmup

For students that would like some extra practice with common linux command and directory navigation using cd, ls, cat, and mv, please look at the optional Pokémon hunt adventure exercise. Students that complete the exercise will earn one extra credit point that can apply to this lab or a future lab assignment. You do not need to complete this adventure to receive full credit on lab 03.

1. Twenty Questions (abbreviated)

In the game 20 questions, one player thinks of a person, place, or thing and others try to guess who, where or what by asking up to 20 questions that can be answered yes or no. Write a program questions.py that plays a simplified version of this game that prompts the user to answer two questions and then guesses an athletic sport.

The two questions to ask are:

  1. Does the sport use a ball (y/n)?

  2. Does the sport use a stick (y/n)?

Stick No Stick

Ball

Lacrosse

Soccer

No Ball

Ice Hockey

Swimming

You may assume the user types in a valid yes/no response as a lowercase y or lowercase n.

$ python3 questions.py
I'll try to guess a sport in only two questions:

Does the sport use a ball (y/n)? y
Does the sport use a stick (y/n)? y

I think the sport is lacrosse

$ python3 questions.py
I'll try to guess a sport in only two questions:

Does the sport use a ball (y/n)? y
Does the sport use a stick (y/n)? n

I think the sport is soccer

2. Movie ticket sales

Write a program movies.py that prompts the user for a number of movie tickets to sell and asks for the ages of the ticket buyers. Ticket prices are based on the chart below. Your program should print the cost of each ticket and the total. You may assume the ages and number of tickets entered by the user are valid non-negative integers.

Type Age Price

Child

12 and under

$10

Adult

13 to 59

$15

Senior

60+

$13

$ python3 movies.py
Welcome to the movie theater sales kiosk

How many movie tickets would you like? 4
Enter the age of the next guest: 7
A child ticket is $10
Enter the age of the next guest: 73
A senior ticket is $13
Enter the age of the next guest: 76
A senior ticket is $13
Enter the age of the next guest: 39
An adult ticket is $15

Your total amount for 4 tickets is $51

3. Character swap

Write a program swap.py that swaps all occurences of a character in a user provided phrase with another character. The characters can be letters, numbers, or symbols. In the case of characters, you can do exact case matching, meaning you do not need to swap both upper case and lower case letters with the equivalent replacement.

$ python3 swap.py
Enter a word or phrase: Jan's new backpack
Enter first symbol to swap: a
Enter second symbol to swap: e

The swapped phrase is below:
Jen's naw beckpeck

$ python3 swap.py
Enter a word or phrase: baba aaa aabbb abbbb
Enter first symbol to swap: a
Enter second symbol to swap: b

The swapped phrase is below:
abab bbb bbaaa baaaa

4. Secret message

In the starter file message.py, a very large string is assigned to the variable msg. Hidden in that string is a secret message. Your job is to find and print the hidden message.

The letters of the hidden message are evenly spaced throughout the long string. The spacing is given by the number of "a"'s in the string. You may assume there is at least one a in the string. To find the hidden message, your first task is to figure out how many "a"'s are in the string. Once you have that number, use it to find the message.

Here’s a small example:

msg = "ssauzatfeiqrbajqrsxwax"

Counting through that string, you’ll find that there are 4 "a"'s. To find the message, start at position 4 (since there are 4 "a"'s) and then use every 4th character (since there are 4 "a"'s). So, here is the secret message, shown with highlighted characters below in msg and with just the secret message letters in secret. You may assume each scrambled message has at least one 'a'.

msg = "ssauzatfeiqrbajqrsxwax"
secret = "zebra"

The only requirements for the program are that you don’t use advanced python concepts (ones we haven’t covered in class yet), and that your program finds and prints the hidden message. When you are getting started, it may help to change the line

msg = long_msg

to

msg = short_msg

This will allow you to test and develop on a small example before switching to the large example. You can also set message to one of the following strings. The example solutions are included.

msg secret

ahi

hi

jaywalk

yak

anaphorically

pray

astronautical

rail

zacas/2!1$

cs21

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!