Free lesson · Module 1, lesson 4 of 8

Printing text and reading error messages

Print text and numbers, add comments, predict what a short program prints line by line, and read a SyntaxError or NameError to find the line and the fix.

The video for this lesson is being prepared

About 10 minutes, narrated in English, Hindi and Odia. The notes and the quiz below are open now.

Lesson notes

What print shows

  • print("Namaste") shows Namaste: the quotes mark where text starts and ends, and are not shown
  • print(250) shows 250: a number needs no quotes
  • print("Total:", 250) shows Total: 250: each comma becomes one space
  • Each print starts a new line, top to bottom
  • A comment starts with #: Python skips the hash sign and the rest of that line. Comments are notes for people

Reading an error message

  • An error is a message, not a failure: Python stops and says what went wrong and where
  • Read the last line first: the kind of error and a short message
  • Then find the line number
  • Syntax error: Python cannot read the line, often a missing quote or bracket (print("Welcome to India) gives SyntaxError: unterminated string literal (detected at line 2) when it is line 2)
  • A syntax error stops the program before any line runs, so nothing is printed
  • Name error: a word Python does not know, such as prnt or Print, or a single word of text without quotes (print(Hello) gives NameError: name 'Hello' is not defined)

The three questions for any error

  • What kind? Read the last line
  • Which line? Find the line number (if that line looks right, check the one above)
  • What is different? Compare it with a line that works: quotes, brackets, spelling, capitals
  • Change one thing, then run again

Try it in Colab or Visual Studio Code

  • # Priya's tiffin bill
  • print("Idli plates:", 2)
  • print("Total:", 60, "rupees")
  • print("Thank you")
  • Expected: Idli plates: 2, then Total: 60 rupees, then Thank you
  • Then break it on purpose: remove one closing quote, or type prnt, and read the message with the three questions

Lesson quiz

Checked in your browser. Nothing is sent or saved.

5 questions

Question 1 · easy

What does this program print?

print("Masala chai")
print(20)

Question 2 · easy

Ravi prints his score. What does this program print?

print("Runs:", 87)

Question 3 · medium

A restaurant prints a short bill. What does this program print?

print("Bill for table", 4)
print("Dosa", 2, "plates")
print("Total", "₹", 140)

Question 4 · medium

What does this program print?

print("100")
print(100)
print("Pin code", "751001")

Question 5 · hard

Nisha runs this program. What does she see?

print("Platform", 2)
print("Train to", "Bhubaneswar")
print(Departs, "at 10")
print("Have a safe journey")

Meet Python and run your first lineNext: Numbers, text and true or false