Python Programming: Ordering App

What is Python?
Python is most popular in 2023
Read more about the popularity of programming languages from around the world at PYPL Index.

Python is currently the most popular programming language, competing with Java, JavaScript, C#, C++, R, PHP, and many others. It is easy to learn and can be used to develop apps and websites, automate boring tasks, analyze data, and to visualize that data. It was developed by Dutch programmer Guido van Rossum in 1991.

In this lesson, you'll learn about Python and practice using the programming concepts you wrote about earlier, by creating a simple ordering app.

Click step 1 to begin.

6.6.1 Launch the code editor
IDE screen before adding code.
The Online-Python IDE is easy to use.

Navigate to Online Python. This Integrated Development Environment (IDE) will allow you to type Python code and 'run' it in a terminal/console to see its output.

  1. Notice the simplicity of the IDE; it allows you to edit, run, save, open, download, and share your code.
  2. If you prefer to view white text on a dark background, then use the Theme (moon) icon to switch from the default dark on white theme.
    Click the Moon icon to change the theme.
  3. Feel free to use the Settings icon to modify other text-editing options.
  4. Click the green Run button to see how the main.py file asks you for two numbers, then calculate them, in the console/terminal. You'll be doing something similar.

To begin your work:

  1. Select and delete the contents of the main.py file.
  2. Copy the following comments (all the text marked with #) and paste it into the empy main.py file:
    
    # CS 101 Ch 6 Shop order made with Python
    # Name:
    # Date:
    # ONID:
    # 6.6.9 Description:
    
    # 6.6.3   Define Shop variables: shop name, 3 lists [] of items, 1 list [] of variations:
    # 6.6.4   Display the lists [] of choices using print():
    # 6.6.5   Take the customer's order using variables and input():
    # 6.6.6   Ask the customer if they want a variation for one of the lists []s:
    #         If yes, then print() that list and ask for their choice using input():
    #         Else, that choices is equal to "no", "none", or something more specific:
    # 6.6.7   Define a function that print()s the customer's order:
    # 6.6.8   Call the function to convert the customer's data inputs into a complete sentence:
    # 6.6.9.4 Optional: Calculate two or more costs and add the total to the customer order sentence.
    
    These are the basic instructions you'll follow. Detail about how to complete each step is provided below and in each tab of this lesson. You'll write your code under each individual green # comment. Code to run will not have a #.
  3. Rename your file with your ONID username.
  4. Click the Save icon to save your file to the Ch 6 folder on your hard drive.
    Click the Save and Open icons to make copy of your work.
  5. Be sure to click the Save icon frequently, so you don't lose your work. Then, next time you need to work on it, click the Open icon to get the file again.
  6. Once you have completed all requirements below, update the Description comment so it tells about your game in a single, descriptive sentence.
  7. To avoid typos, ctlc copy and ctrlv paste the required green code comments (above) in order to get started. And, copy the green code samples in the remaining tabs of this activity. You'll revise them to fit your app.
6.6.2 Understand the requirements.

Your finished app should include the following features:

  • Python comments to explain each block of code, using #.
  • A set of shop variables, such as the
    • __Shop name.
    • __3 lists of items for sale.
    • __Choices/variations for 1 or more of those items.
  • Requests of input() from customers to determine their item choices.
  • A conditional if/else statement for handling customer decisions about a variation/option.
  • A function to print out the final shop order, using def.
  • Extra Credit: Calculate the total cost of the order.

To give you an idea of the scope of your work, view the following example. Notice that it has a menu of choices, asks the customer for input, asks for a choice to be made, offers more choices, and prints all the customer's input into a complete sentence:

Output from the instructor's app shows inputs for taking a drink order and the final order.
Your app will look different! You must use your imagination to choose a topic that is not the same as the example above. Here are some ideas: meal, car, clothing/shoes, equipment, supplies, flowers, etc. Choose a theme and stick with it.

Remember these Python writing conventions:
  • As you type in the IDE's editing screen, you'll notice that it completes some of the syntax, such as quote marks and brackets.
  • Comments begin with a #. They describe what each block of code is doing.
  • Lists of items for sale are nested inside brackets [] and each string value in a list is surrounded by quotemarks " ".
  • Function and variable names are typed in snake_case, which separates words with an underscore.
  • Functions and if/else statements require a colon at the end of the first line and an indent for their arguments.
  • Python is case-sensitive.
6.6.3 Define Variables

Variables are names that represent a set of data. For example, if you had a list of drinks available in a food cart, the variable name could be drinks and the data list could be tea, coffee, water. Variables can be a string of words, a number, a date, a boolean (yes/no), or an object/list. The name goes on the left side of an equals = operator and the value for it goes on the right between quote marks (if it is a string of text; numbers don't need quote marks).

  1. Define a variable name that is equal to the name of your shop, like this:

    
    shop_name = "Caffeinated!"
    
    Change the value for shop_name to your shop's name.

Simple? Let's create more, using lists! Lists require the use of brackets, quote marks, and commas, like this: ["value1", "value2", "value3", "value4"]: The Caffeinated shop offers drink_types like this:


drink_types = ["cocoa", "black tea", "white tea", "green tea", "espresso", "coffee", "water"]
And, it offers these options of milk_types:

milk_types = ["dairy", "soy", "oat", "cashew", "coconut", "macadamia", "whipped", "none"]  

  1. Make a new line in your file and define names and values for four lists (change these names and values to fit your topic):
    1. A list of items for sale.
      
      list_one = ["value1", "value2", "value3", "value4"]
      
    2. A second list of items for sale.
      
      list_two = ["value5", "value6", "value7", "value8"]
      
    3. A third list of items for sale.
      
      list_three = ["value9", "value10", "value11", "value12"]
      
    4. Options for one of the lists, such as size, color, weight; something topic-specific, etc.
      
      option_list = ["value13", "value14", "value15", "value16"]
      
6.6.5 Take the customer's order input.

Python's built-in input() function allows the programmer to prompt the customer to enter some data. Each prompt becomes a variable, so we can use its name to perform instructions. For example, asking the customer what type of drink they want looks like this:


drink_type = input("What kind of drink would you like?")
Notice the variable name is equal to the input statement that is wrapped in quote marks and parenthesis.

Use that same syntax to create variables with input prompts for your three lists. The first one is done for you, but change the variable name and 'item' to fit your topic:

  1. first list
    
    order_type = input("What kind of item would you like? ")
    
  2. second list
  3. third list

Test: click the Run button again:

  1. Did your menu and input prompts print as intended?
  2. If your lists don't print as intended, then go back to your program and fix:
    • quote marks
    • brackets and parentheses
    • commas
  3. Edit the prompts so they create complete, interrogative sentences.
  4. Check your spelling.
6.6.6 Prompt the customer to make a choice with an if/else statement.

Sometimes an item has variations to choose from. We can accommodate variations using an if statement. For example, if the customer wants milk, then they can choose from a variety of milks. else they can choose no milk. We can write that in Python by first prompting the customer for the variations with order_milk, so they can input their option of y for yes and n for no. If they type y, then we can print the Options list that has those milk choices. Then, they can type their choice of milk. If they choose n, then the else statement takes over and changes the status of the milk to no milk, like this:


order_milk = input("Would you like to add milk? (y/n) ")
if "y" in order_milk.lower():
    print("Available milks:", milk_types)
    milk = input("What milk would you like? ")
else:
    milk = "no milk"
If statments use colon>s : and indentation to help the program understand which statements go with which function. Notice that the if line has a colon at the end, then the next lines are indented. The same goes for the else. A colon is used along with indentations.

Sometimes the customer might type their option in uppercase letters, which we can change to lowercase letters using the built-in function .lower(). Lowercase is needed to render proper grammar in the final order.

Give it a try! Based on your Options list of variations for one item, give the customer a choice:

  1. Define a variable for prompting the customer with an input to make a choice of yes or no:
    
    order_option = input("Would you like to add an option? (y/n) ")
    
  2. Define an if statement that takes y for "yes", converts their input to lowercase letters, and ends with a colon.
    
    if "y" in order_option.lower():
    
  3. Indent one tab and print() the Options list.
    
        print("Available options:", option_list)
    
  4. Indent one tab and define a variable to ask which of those Options the customer wants, using input():
    
        option = input("What option would you like? ")
    
  5. Define an else:
    
    else:
    
  6. Indent one tab and redefine that last varible so it specifies none of the Options in the list:
    
        option = "no options"
    
  7. Test: click the Run button to test the if/else: statements. Answer yes to make the choice, then test again responding with no. If the options did not work correctly, go back to your program and fix:
    • quote marks
    • brackets and parentheses
    • commas
    • spelling
6.6.7 Define a function to print the final order

A function defines a set of instructions for the program to complete. In your shop program, the ultimate goal is to print out the final order. Python functions begin with def (define), a name to call it with, and to return something. In this case, it is the function itself.

The function will need access to the customer's input, and we can do that by passing in names that represent the customer's choices. Those names go between the function's parenthesis: drink_order(), like this:


def drink_order(size, temperature, kind, milk, sweetener):
    print(f"Here is your {size} {temperature} {kind} drink with {milk} and {sweetener}.")
    return

Notice that the names we use to pass in those values came from the customer's input, rather than our original variable definitions. The data input by the customer is what we want to render in the final order print statement.

Notice also that the print statement uses an f string to allow full use of text and variables in the same statement. Now you try it:

  1. Define the name of your order function:
    
    def shop_order()
    
  2. Within the functions parenthesis (), list the names that represent the items the customer ordered:
    
    shop_order(name1, name2, name3, option1)
    
  3. Indent one tab and print() a sentence that uses those passed-in names. Each name must be enclosed in curley braces {}:
    
        print(f"Here is your {name1} {name2} {name3} item with {option}.")
    
    Add commas between the passed-in values, if necessary.
  4. Indent one tab and type return:
    
        return
    
6.6.8 Call the function to display the final order.

The function will complete its instructions when we "call" it. We do that by stating its name and passing in the values chosen by the customer, like this:


drink_order(size, temp, type, milk, sweetener)

Pretend you are the customer now, and place an order:

  1. Type the name of the function:
    
    def shop_order(name1, name2, name3, option1)
    
  2. Click the Run button.
  3. Did your menu print as intended?
  4. Were you able to input your choices after each prompt?
  5. Did the final order print as intended with proper syntax and sentence structure?
  6. If your prompts and values don't print as intended, then go back to your program and fix:
    • quote marks
    • brackets, parentheses, and curley braces
    • colons and commas
    • spelling
  7. Edit the prompts so they create complete interrogative sentences.
  8. Edit the final print statement to use correct spelling, spacing, and punctuation.
6.6.9 Add comments and extra options.

Let's add the finishing touches and add a calculation, if you have time.

  1. Update the Description comment at the top of your file to fit your topic. Write the description in a complete sentence:
     
    # Description: 
    
  2. (Optional) To improve readability of your output in the terminal/console, add borders around the menu and the final message, using print() statements with special characters and the "new line" \n code, like this:
    
    print("\n *--*--*--*-- \n").
    
    Use different symbols than this example.
  3. (Optional Extra Credit) If you want to add the total cost of the order to the final message, then you can:
    1. Define a dictionary, for one or more of your lists, something like this:
      
      size_cost = {
        "small": 1.00,
        "medium": 2.00,
        "large": 3.00,
      }
      
      Python dictionaries define sets of parameters and values enclosed in curley braces {}.
    2. Define a variable for the cost of your variation(s). For example, in the Caffeinated shop's if statement for adding milk, the milk_cost of .50 cents is defined like this:
       
      if "y" in order_milk.lower():
        print("Available milks:", milk_types)
        milk_cost = 0.50
      
    3. Then, we can define a variable to calculate the cost of the dictionary value size and milk_cost. The values related to the parameters can be accessed using its variable name and brackets [] like this:
      
      total = size_cost[size] + milk_cost
      
    4. That variable total can be added to the function's passed-in values and print statement, like this:
      
      def drink_order(size, temperature, kind, milk, sweetener, total):
          print(f"Here is your {size} {temperature} {kind} drink with {milk} and {sweetener} for a total cost of {total}.")
          return
      
6.6.10 Get the sharing URL.
  1. Save your code one last time by clicking the Save icon. Save it to your hard drive so you can retrieve it if necessary.
  2. Click the share button to copy the sharing URL.
    Click share to get the sharing URL. Click share to get the sharing URL.
    • Test that share button to ensure it works (by not deleting your work at the IDE).
    • If it doesn't work, then upload your saved file to this IDE: Trinket for Python 3
    • Share the link using the toggle menu's Share option or by using the sharing icon at the top.
  3. Paste the URL somewhere for easy access. You'll be using it for a hyperlink in your discussion post.

Go back to the Ch 6 Discussion, step 6.7.