CS21 Lab 3: if statements, for loops

Due Saturday, October 3, before midnight

Programming Tips

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

  • 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. In emacs, at the bottom, center of the window, there is an indication of both the line and the column of the cursor.

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

  • use formatted printing

1. Checkpoint

At the end of your lab session, be sure to run handin21.

Your lab instructor will check your files, which should reflect that you have made non-trivial progress (in python or pseudocode) towards the solution. Note that if you have not made much progress towards your solution, we expect that you would have been actively seeking help during your lab over Slack.

A portion of your final lab grade is dependent on this checkpoint.

If there are circumstances which prevent you from making substantial progress on this lab, please contact your lab instructor as soon as possible.

2. Topic Quiz

Write a program called topicQuiz.py that will run a mini quiz for the user. Here’s one example of a possible topic quiz. You will select a topic that appeals to you. The user input is shown in bold.

$ python3 topicQuiz.py
This is a quiz about the game of ultimate.
Let's test your knowledge...

1. In ultimate, a frisbee is called a disc (y/n)? y
You are correct!
2. The game of ultimate is self-officiated (y/n)? n
Sorry, that is the wrong answer.
3. Ultimate is where people throw frisbees to dogs (y/n)? y
Sorry, that is the wrong answer.
4. In ultimate, you can run with the disc (y/n)? n
You are correct!
5. In ultimate, you can throw a scoober or hammer (y/n)? y
You are correct!

You got 3 out of 5 correct.
That's pretty good, but you could do better!

The output for your program will depend on your topic of choice, though your output should look similar in structure. Here are the requirements for the program:

  • Your quiz must consist of at least 5, but not more than 10, questions. You will use a for loop to ask the questions.

  • Your quiz may be on any topic you wish, but the questions must have "yes" or "no" answers.

  • You must store the questions to your quiz in a list of strings. Call this list questions. For example, the list of questions for the ultimate quiz looks like this:

    questions = ["In ultimate, a frisbee is called a disc",
                 "The game of ultimate is self-officiated",
                 "Ultimate is where people throw frisbees to dogs",
                 "In ultimate, you can run with the disc",
                 "In ultimate, you can throw a scoober or hammer"]
  • You must use string formatting to print out each question with a number at the beginning and a "(y/n)?" at the end.

  • You must store the answers to your quiz in a list of strings, where each string is either "y" for "yes" or "n" for "no". Call this list answers. For example, the list answers for the ultimate quiz looks like this:

    answers = ["y", "y", "n", "n", "y"]
  • You may assume that the user always gives valid input.

  • When the quiz is over, your program must print out the score and a suitable congratulatory or encouraging message.

  • You must have at least 3 different messages: for a perfect score (e.g. "You’re an expert!"), for a decent score (e.g. "You could do better."), and for a low score (e.g. "You need to study up!").

3. Pizza Order

Write a program called pizzaOrder.py that will take a pizza order, and calculate the total cost. The cost depends on the size as well as the number and type of toppings you choose. Here’s a sample run of the program where user inputs are shown in bold:

$ python3 pizzaOrder.py
Welcome to the Pizzeria!
A large pizza is $10.25
A small pizza is $ 8.50
Veggie toppings (spinach, peppers, mushrooms) are $1.00 each
Meat toppings (pepperoni, sausage, ham) are $2.00 each
------------------------------------------------------------
Size large or small? small
How many toppings do you want? 3
Topping 1: sausage
Topping 2: onions
Sorry, we don't offer that topping!
Topping 3: peppers
------------------------------------------------------------
You ordered a small pizza with sausage peppers
Your total is $11.50
Thanks, and come again!

Notice in the above example that the user was not charged for the invalid topping choice. The cost was $8.50 for the small pizza, plus $2.00 for the sausage, and another $1.00 for the peppers, for a total of $11.50.

Here’s another example run:

$ python3 pizzaOrder.py
Welcome to the Pizzeria!
A large pizza is $10.25
A small pizza is $ 8.50
Veggie toppings (spinach, peppers, mushrooms) are $1.00 each
Meat toppings (pepperoni, sausage, ham) are $2.00 each
------------------------------------------------------------
Size large or small? large
How many toppings do you want? 0
------------------------------------------------------------
You ordered a large pizza with no additional toppings
Your total is $10.25
Thanks, and come again!

Here are the requirements for the program:

  • You can assume that the user chooses a valid pizza size and enters a valid number for how many toppings they’d like.

  • If the user enters an invalid topping choice, tell them, and don’t charge anything for that topping.

  • Print out the final description of their order including the size and all valid topping choices. If they don’t add any toppings, have a special message for that case.

  • Use formatted printing to show the total amount with exactly two decimal places.

4. Optional Extra Challenge

This part does not affect your grade, so only attempt this after completing the rest of your lab. It is simply an extra challenge, if you want to try it.

Update your topicQuiz.py program so that it:

  • Asks the user how many questions they’d like to attempt within a valid range of possible choices, say from two up to the total number of questions available.

  • Use the randrange function from the random library to randomly select which questions to ask. Don’t worry if you happen to select the same question multiple times.

5. 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, run handin21 again.

Turning in your labs…​.

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.