Example: Programming Languages and a Ticket Total

A programming language is a formal system for expressing instructions with specified rules and meanings. Its syntax is the set of rules governing how symbols and statements may be arranged. You need both a permitted arrangement and instructions that express your intended calculation.


A school theatre sells eight tickets at 7 dollars each and charges one booking fee of 3 dollars for the entire order. A student writes the following source code in Python, but one symbol is missing.


Python
tickets = 8
price = 7
fee = 3
total = tickets price + fee
print(total)


For this example, = stores the value on its right under the name on its left. The symbol * means multiplication, and multiplication occurs before addition. The instruction print(total) displays the stored value of total. All monetary values in the fragment represent dollars.


Program text passes to Python software, which carries out valid instructions and produces displayed output.


a) Repair the line defining total. Explain why Python cannot use the original line as written.


The intended ticket cost is the number of tickets multiplied by the price of each ticket. Python requires an explicit multiplication operator between these two names; placing them next to each other does not express multiplication.


The corrected line is:


Python
total = tickets * price + fee


The original line breaks Python's syntax rules because the required operator between tickets and price is missing. Supporting software cannot carry out that line as a valid Python assignment.


b) After this repair, what number does the program display, and what does it represent?


First calculate the ticket subtotal. Multiplication is evaluated before the booking fee is added:


ticket subtotal=8×7=56 dollars\text{ticket subtotal} = 8 \times 7 = 56\ \text{dollars}


The intermediate ticket subtotal is ________. Add the single booking fee to obtain the order total:


order total=56+3=59 dollars\text{order total} = 56 + 3 = 59\ \text{dollars}


The assignment stores 59 in total, and print(total) displays 59. This represents an order total of 59 dollars; the program does not add a currency label to the displayed number.


c) The student instead changes the repaired line to total = tickets * (price + fee). Find the new displayed number and decide whether this valid expression follows the stated pricing rule.


Parentheses require the program to add price and fee first:


grouped amount=7+3=10 dollars\text{grouped amount} = 7 + 3 = 10\ \text{dollars}


The grouped amount is ________. The program then multiplies that amount by the number of tickets:


stored total=8×10=80 dollars\text{stored total} = 8 \times 10 = 80\ \text{dollars}


The program displays 80, representing 80 dollars. This expression is valid Python, but it applies the booking fee to every ticket. The stated rule charges that fee once for the entire order, so this expression does not implement the intended price.


Valid syntax does not guarantee the intended result. Check both whether the language accepts the instructions and whether their meaning matches the task.

A student wants a computer to calculate watering times for a community garden. Why would the student express the instructions in a programming language rather than an informal note?

Choose an answer

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

A student creates and revises instructions for a school announcements app. Later, a computer carries out those instructions to display the announcements.


Use the words programming and execution once each to complete the descriptions.


Creating and revising the app's instructions is . The computer carrying out the instructions that already exist is .

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

A beginner writes instructions, checks their arrangement, and runs them to display a message. Match each programming term to its description.

Match each term
A.

A formal system for expressing instructions with specified rules and meanings

B.

Rules governing how symbols and statements may be arranged

C.

Information produced when instructions are executed

D.

Program text written in a particular language

Source code

Program output

Programming language

Syntax

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

Extra Practice