Chapter 6 Activity Instructions

Chapter Six: Lists and Loops

Activity: Burger Castle

 

Burger, fries and sodaIn this chapter, you how to create lists and tuples that contain multiple data values under one variable name. You also learned how to iterate over those data values with "for" loops and create "while" loops that continue so long as a condition is True.

You are going to use all those skills to create the "BurgerCastle" fast food ordering system! Your "BurgerCastle" program will show the customer a menu, take individual items in their order, and then print out a description of each item in the order.

The sample run below demonstrates how the program should work.

Welcome to Burger Castle
Menu:  ('burger', 'fries', 'salad', 'soda', 'milkshake')
Please enter each item in your order.  Press 'Enter' or type 'quit' on an empty line when done
Enter Item: burger
Enter Item: soda
Enter Item: pizza
Sorry, we don't sell pizza
Enter Item: fries
Enter Item: quit

Order complete; you are having:
Half-pound burger
Diet root beer
Large fries
Thanks for visiting Burger Castle!

 

Detailed Program Requirements

Write your program by carefully following the steps listed below. For the best grade, please match the expected output exactly (including correct spelling and case of all messages).

  1. Start with a comment that contains your student name
  2. Initialize a tuple named validOrders with the contents "burger","fries","salad","soda","milkshake". This tuple contains the allowed strings that a customer can type to add an item to the order.
  3. Initialize a tuple named itemDescriptions with the contents: "Half-pound burger","Large fries","Side salad", "Diet root beer", "Chocolate shake". This tuple contains a description of each of the allowed orders.

Note that values in the validOrders and itemDescriptions tuples are arranged so matching items and descriptions will always have the same numeric index value. This means if you find the index value for a validOrders element like "fries" (index 1), you can get the description by reading the value at the same index from the itemDescriptions.

Matching tuple index values

  1. Next, initialize an empty list named order. This list will grow as customers enter each item they want to buy.
  2. Next, print three messages to the screen:
    • "Welcome to Burger Castle"
    • "Menu: " (and then the list of validOrders)
    • "Please enter each item in your order. Press 'Enter' or type 'quit' on an empty line when done."
Checkpoint: If you run your program now, you should see the first three lines displayed as shown below.
Welcome to Burger Castle
Menu:  ('burger', 'fries', 'salad', 'soda', 'milkshake')
Please enter each item in your order.  Press 'Enter' or type 'quit' on an empty line when done.

Next, you need to write a "while" loop that will prompt the customer for an item, continuing to do so until the customer wants to quit. If the item is valid, it should be added to the orders list, otherwise an error message is printed. We recommend using the "do-while" style, where the loop will prompt the user once at the top, break out of the loop if the user is done, and otherwise process the item at the end of the loop. However, you are free to write any "while" loop that meets the program's input and output requirements.

The following tasks should be completed by your "while" loop. The exact order and logic will depend on your chosen "while" loop details.

  1. Prompt the user for input() with "Enter Item: " and store the result in the item variable.
  2. If the item is an empty string ("") or the value "quit", the loop should end without adding that item to the order list.
  3. If the item is present in the validOrders tuple, append that item to the end of the order list.
  4. Else if the item is not in the validOrders, print a message to the screen that says "Sorry, we don't sell " + item.

 

Checkpoint: If you run your program now, you should be able to successfully enter any valid item on the menu and should receive the correct error message if you enter an item that is not on the menu.
Welcome to Burger Castle
Menu:  ('burger', 'fries', 'salad', 'soda', 'milkshake')
Please enter each item in your order.  Press 'Enter' or type 'quit' on an empty line when done
Enter Item: burger
Enter Item: soda
Enter Item: pizza
Sorry, we don't sell pizza
Enter Item: fries
Enter Item: quit
  1. Next, after the "while" loop, add some print() statements to display a blank line and the phrase "Order complete; you are having:" to the screen. The blank line and message should appear after the user exits the "while" loop.
Enter Item: quit

Order complete; you are having:

Finally, you need to write a "for" loop over each of the items in the order list. Find the index value of that item in the validOrders tuple, and then read and print the matching description of that item from the itemDescriptionstuple.

  1. Start a "for" loop were the loop variable name is item and you are iterating over all the values in the order list.
  2. Use the index() function to search the validOrders tuple for the numeric index of the current item, and store that number in the index variable.
  3. Read the data at that index from the itemDescriptions tuple and store it in a variable named description. Remember, you can use the square brackets notation itemDescriptions[index] to read a value from a specific index position in a list or tuple.
  4. Print the description to the screen
  5. After the "for" loop is done, print the final message "Thanks for visiting Burger Castle!".

 

Checkpoint: Your program should now be fully functional and print the correct descriptions for each of the items the customer ordered. Test your program with each of the suggested input combinations shown below.

 

Starting Code

We are not providing any starting code for this program. It is important for you to practice turning detailed project requirements into a working program on your own. That way, when you have an idea for your own program, you won't need any help writing it from scratch!

Note that we suggested periodic "checkpoints" in your work to verify one section of code works before moving to the next section. This approach is important for software engineers! It is much easier to write and test programs in small chunks. If you spend lots of time writing a large program and then try to run it all at once, you will likely have a harder time finding and fixing problems that could be anywhere in your code.

 

Activity Results

The following example runs demonstrate some possible input and output combinations. Be sure your program matches the desired output exactly in each case for the best grade (double-check spelling, capitalization, etc.). You should be able to test with other combinations of input and get similar outputs, based on your understanding of the program behavior.

Test #1 - Items: quit (no items - just quit right away)

Welcome to Burger Castle
Menu:  ('burger', 'fries', 'salad', 'soda', 'milkshake')
Please enter each item in your order.  Press 'Enter' or type 'quit' on an empty line when done
Enter Item: quit

Order complete; you are having:
Thanks for visiting Burger Castle!

Test #2 - Items: burger, quit

Welcome to Burger Castle
Menu:  ('burger', 'fries', 'salad', 'soda', 'milkshake')
Please enter each item in your order.  Press 'Enter' or type 'quit' on an empty line when done
Enter Item: burger
Enter Item: quit

Order complete; you are having:
Half-pound burger
Thanks for visiting Burger Castle!

Test #3 - Items: fries, "" (empty string)

Welcome to Burger Castle
Menu:  ('burger', 'fries', 'salad', 'soda', 'milkshake')
Please enter each item in your order.  Press 'Enter' or type 'quit' on an empty line when done
Enter Item: fries
Enter Item: 

Order complete; you are having:
Large fries
Thanks for visiting Burger Castle!

Test #4 - Items: burger, soda, pizza, quit

Welcome to Burger Castle
Menu:  ('burger', 'fries', 'salad', 'soda', 'milkshake')
Please enter each item in your order.  Press 'Enter' or type 'quit' on an empty line when done
Enter Item: burger
Enter Item: soda
Enter Item: pizza
Sorry, we don't sell pizza
Enter Item: quit

Order complete; you are having:
Half-pound burger
Diet root beer
Thanks for visiting Burger Castle!

Test #5 - Items: fries, milkshake, burger, soda, salad, quit

Welcome to Burger Castle
Menu:  ('burger', 'fries', 'salad', 'soda', 'milkshake')
Please enter each item in your order.  Press 'Enter' or type 'quit' on an empty line when done
Enter Item: fries
Enter Item: milkshake
Enter Item: burger
Enter Item: soda
Enter Item: salad
Enter Item: quit

Order complete; you are having:
Large fries
Chocolate shake
Half-pound burger
Diet root beer
Side salad
Thanks for visiting Burger Castle!

Test #6 - Items: candy, candy, burger, burger, soda, soda, quit

Welcome to Burger Castle
Menu:  ('burger', 'fries', 'salad', 'soda', 'milkshake')
Please enter each item in your order.  Press 'Enter' or type 'quit' on an empty line when done
Enter Item: candy
Sorry, we don't sell candy
Enter Item: candy
Sorry, we don't sell candy
Enter Item: burger
Enter Item: burger
Enter Item: soda
Enter Item: soda
Enter Item: quit

Order complete; you are having:
Half-pound burger
Half-pound burger
Diet root beer
Diet root beer
Thanks for visiting Burger Castle!

 

When your program is complete and tested, submit it for grading!

Activity Rubric

If your project is being automatically graded by our system, your grade will be calculated from 0 to 100 as follows:

PointsDescription
5validOrders tuple declared and initialized with correct values
5itemDescriptions tuple declared and initialized with correct values
5order list declared and initialized as empty
5"while" loop is created and used
5"for" loop is created and used
5append() function is used on order list
5index() function is used on validOrders tuple
5matching index used to read description from itemDescriptions
10Test Case #1 passes
10Test Case #2 passes
10Test Case #3 passes
10Test Case #4 passes
10Test Case #5 passes
10Test Case #6 passes

 

End of Activity


Last modified: Wednesday, 8 May 2019, 9:01 AM