Lesson 2

Displaying Text on the Screen and Input

csinschools.com

Previously, on CS in Schools...

  • What is the keyword you use to display text onto the screen?
  • What would you type to display CS in Schools on the screen?

2

Answer:

print()

CS in Schools - Python Reference Sheet

Learning objectives

  • Understand more about printing output on the screen with Python
  • Understand how spaces and lines are used in Python
  • Use input() to wait for the ENTER key to be pressed

4

Would you like to see a magic trick?

5

Would you like to see a magic trick?

Click here and run the program

Did you figure it out?

6

Take a look at the code...

Notice all the blank lines?

7

7

print("I am going to do a magic trick")

input("[Press enter to continue]")

print("Secretly pick one of the cards below..")

print("K\u2664 J\u2667 Q\u2661 J\u2662")

print("\nYou got it? Don't tell anyone!")

input("[Press enter to continue]")

print("\nNow... Your card will disappear!")

print("Continue focusing on your card!")

input("[Press enter to continue]")

print("Can you now find your card below?")

print("J\u2664 K\u2667 Q\u2662 J\u2661 ")

input("[Press enter to continue]")

print("\nIt's magic.....!")

input("[Press enter to end]")

Take a look at the code...

Notice all the blank lines?

8

8

print("I am going to do a magic trick")

input("[Press enter to continue]")

print("Secretly pick one of the cards below..")

print("K\u2664 J\u2667 Q\u2661 J\u2662")

print("\nYou got it? Don't tell anyone!")

input("[Press enter to continue]")

print("\nNow... Your card will disappear!")

print("Continue focusing on your card!")

input("[Press enter to continue]")

print("Can you now find your card below?")

print("J\u2664 K\u2667 Q\u2662 J\u2661 ")

input("[Press enter to continue]")

print("\nIt's magic.....!")

input("[Press enter to end]")

Take a look at the code...

Notice all the blank lines?

How important do you think they are?

9

9

print("I am going to do a magic trick")

input("[Press enter to continue]")

print("Secretly pick one of the cards below..")

print("K\u2664 J\u2667 Q\u2661 J\u2662")

print("\nYou got it? Don't tell anyone!")

input("[Press enter to continue]")

print("\nNow... Your card will disappear!")

print("Continue focusing on your card!")

input("[Press enter to continue]")

print("Can you now find your card below?")

print("J\u2664 K\u2667 Q\u2662 J\u2661 ")

input("[Press enter to continue]")

print("\nIt's magic.....!")

input("[Press enter to end]")

Spaces in Python code

  • Generally, Python is okay with spaces and blank lines anywhere in your code to improve its presentation and readability
  • In code, we refer to spaces and blank lines as “whitespace”
  • Why would you add whitespace to your code?

10

Spaces in Python code

  • Why would you add whitespace to your code?
    • It makes it easier for you to read your own code
    • You may forget what your own program does over time, and good layout helps you figure it out again
    • It also helps other people read your code. Companies such as Google and Microsoft have rules about how your code should look
    • Your teacher will appreciate you making your code readable!

11

These programs are the same

12

print("I am going to do a magic trick")

input("[Press enter to continue]")

print("Secretly pick one of the cards below..")

print("K\u2664 J\u2667 Q\u2661 J\u2662")

print("\nYou got it? Don't tell anyone!")

input("[Press enter to continue]")

print("\nNow... Your card will disappear!")

print("Continue focusing on your card!")

input("[Press enter to continue]")

print("Can you now find your card below?")

print("J\u2664 K\u2667 Q\u2662 J\u2661 ")

input("[Press enter to continue]")

print("\nIt's magic.....!")

input("[Press enter to end]")

print("I am going to do a magic trick")

input("[Press enter to continue]")

print("Secretly pick one of the cards below..")

print("K\u2664 J\u2667 Q\u2661 J\u2662")

print("\nYou got it? Don't tell anyone!")

input("[Press enter to continue]")

print("\nNow... Your card will disappear!")

print("Continue focusing on your card!")

input("[Press enter to continue]")

print("Can you now find your card below?")

print("J\u2664 K\u2667 Q\u2662 J\u2661 ")

input("[Press enter to continue]")

print("\nIt's magic.....!")

input("[Press enter to end]")

These programs are the same

13

print("I am going to do a magic trick")

input("[Press enter to continue]")

print("Secretly pick one of the cards below..")

print("K\u2664 J\u2667 Q\u2661 J\u2662")

print("\nYou got it? Don't tell anyone!")

input("[Press enter to continue]")

print("\nNow... Your card will disappear!")

print("Continue focusing on your card!")

input("[Press enter to continue]")

print("Can you now find your card below?")

print("J\u2664 K\u2667 Q\u2662 J\u2661 ")

input("[Press enter to continue]")

print("\nIt's magic.....!")

input("[Press enter to end]")

print("I am going to do a magic trick")

input("[Press enter to continue]")

print("Secretly pick one of the cards below..")

print("K\u2664 J\u2667 Q\u2661 J\u2662")

print("\nYou got it? Don't tell anyone!")

input("[Press enter to continue]")

print("\nNow... Your card will disappear!")

print("Continue focusing on your card!")

input("[Press enter to continue]")

print("Can you now find your card below?")

print("J\u2664 K\u2667 Q\u2662 J\u2661 ")

input("[Press enter to continue]")

print("\nIt's magic.....!")

input("[Press enter to end]")

Spaces in Python code

  • Generally, Python is okay with whitespace in your code to improve readability and presentation
  • This isn’t true when the whitespace is in between quotes “” or in the middle of special words (such as print)
  • Why do you think whitespace matters when it’s inside quotes or in special words?

14

Spaces in Python code

print ( “Hello, world!” )

15

Spaces in Python code

print ( ”Hello, world!” )

16

Spaces in Python code

print ( ”Hello, world!” )

17

Spaces between words and non-quotes do not matter

Spaces in Python code

print ( “Hello, world!” )

18

Spaces in Python code

print ( “Hello, world!” )

19

Hello world!

Spaces in Python code

print ( “Hello, world!” )

20

Hello world!

Spaces in Python code

print ( “Hello, world!” )

21

Hello, world!

All done!

Spaces in Python code

print(“Hello , world !”)

22

Spaces in Python code

print(“Hello , world !”)

23

Spaces in Python code

print(“Hello , world !”)

24

But spaces between quotation marks do matter

Spaces in Python code

print(“Hello , world !”)

25

Spaces in Python code

print(“Hello , world !”)

26

Hello , world !

Spaces in Python code

print(“Hello , world !”)

27

Hello , world !

Spaces in Python code

print(“Hello , world !”)

28

Hello , world !

All done!

Spaces in Python code

prin t(“Hello, world!”)

29

Spaces in Python code

prin t(“Hello, world!”)

30

Spaces in Python code

prin t(“Hello, world!”)

31

This will NOT work!

Activity: Error messages

  • Believe it or not, your programs usually won’t work when you write them!
    • It doesn’t matter how experienced you are, this always happens
  • A bug is something that goes wrong in your program
  • Sometimes, when something goes wrong, Python can detect it and tell you about it through error messages
  • In this exercise, we are going to get used to reading Python error messages by deliberately making mistakes!

32

Complete this table of errors

33

Copy this code line into Repl

What error message did you see displayed on the screen when you ran it?

(Some of the code may actually work!)

What did you type in to fix the code?

(If the code works, you won’t need to type anything)

prit("Hello, world!")

NameError: name 'prit' is not defined

print("Hello, world!")

Complete Repl exercise 02.01 - Buggy Code

Programming… one line at a time, top to bottom!

  • Just like humans, computers read code one line at a time
  • Computers start at the top of the screen and work their way to bottom of the screen
  • Computers will always follow your instructions, even if your instructions are wrong or don’t do what you intended

34

One line at a time

print(“Hello, Alice!”)

print(“Hello, Bob!”)

35

One line at a time

print(“Hello, Alice!”)

print(“Hello, Bob!”)

36

One line at a time

print(“Hello, Alice!”)

print(“Hello, Bob!”)

37

Hello, Alice!

One line at a time

print(“Hello, Alice!”)

print(“Hello, Bob!”)

38

Hello, Alice!

One line at a time

print(“Hello, Alice!”)

print(“Hello, Bob!”)

39

Hello, Alice!

One line at a time

print(“Hello, Alice!”)

print(“Hello, Bob!”)

40

Hello, Alice!

Hello, Bob!

One line at a time

print(“Hello, Alice!”)

print(“Hello, Bob!”)

41

Hello, Alice!

Hello, Bob!

One line at a time

print(“Hello, Alice!”)

print(“Hello, Bob!”)

42

Hello, Alice!

Hello, Bob!

All done!

Computers do what they’re told...

43

One line at a time

print(“Do this last!”)

print(“Don’t display me.”)

print(“Show me first.”)

44

One line at a time

print(“Do this last!”)

print(“Don’t display me.”)

print(“Show me first.”)

45

One line at a time

print(“Do this last!”)

print(“Don’t display me.”)

print(“Show me first.”)

46

One line at a time

print(“Do this last!”)

print(“Don’t display me.”)

print(“Show me first.”)

47

Do this last!

One line at a time

print(“Do this last!”)

print(“Don’t display me.”)

print(“Show me first.”)

48

Do this last!

One line at a time

print(“Do this last!”)

print(“Don’t display me.”)

print(“Show me first.”)

49

Do this last!

Don’t display me!

One line at a time

print(“Do this last!”)

print(“Don’t display me.”)

print(“Show me first.”)

50

Do this last!

Don’t display me!

One line at a time

print(“Do this last!”)

print(“Don’t display me.”)

print(“Show me first.”)

51

Do this last!

Don’t display me.

Show me first.

One line at a time

print(“Do this last!”)

print(“Don’t display me.”)

print(“Show me first.”)

52

Do this last!

Don’t display me.

Show me first.

One line at a time

print(“Do this last!”)

print(“Don’t display me.”)

print(“Show me first.”)

53

Do this last!

Don’t display me.

Show me first.

All done!

Using input() to wait for the user

  • In our card trick at the start of the class, we waited for the user to press ENTER before continuing
  • Why might this be useful in your own programs?

54

Using input() to wait for the user

  • We use input() to ask the user to type in information into our program
  • It will let the user type in anything they want until they press the ENTER key
  • In this case, we don’t really care what they type in, as long as they type in ENTER at the end

55

Using input() to wait for the user

input(“Please press [ENTER] to continue”)

56

Using input() to wait for the user

input(“Please press [ENTER] to continue”)

57

Using input() to wait for the user

input(“Please press [ENTER] to continue”)

58

Please press [ENTER] to continue

Using input() to wait for the user

input(“Please press [ENTER] to continue”)

59

Please press [ENTER] to continue

Using input() to wait for the user

input(“Please press [ENTER] to continue”)

60

Please press [ENTER] to continue

Using input() to wait for the user

input(“Please press [ENTER] to continue”)

61

Please press [ENTER] to continue

Using input() to wait for the user

input(“Please press [ENTER] to continue”)

62

Please press [ENTER] to continue

Using input() to wait for the user

input(“Please press [ENTER] to continue”)

63

Please press [ENTER] to continue

Using input() to wait for the user

input(“Please press [ENTER] to continue”)

64

Please press [ENTER] to continue

All done!

Activity: Super smiley face!

Complete Repl.it Activity:

02.02 - Super Smiley Face

65

Activity: Super smiley face!

Complete Repl.it Activity:

02.02 - Super Smiley Face

66

Activity: Fuzzy Hedgehog

Complete Repl.it Activity:

02.03 - Fuzzy Hedgehog

67

Activity: Fuzzy Hedgehog

Complete Repl.it Activity:

02.03 - Fuzzy Hedgehog

68

Tutorial Video: Fuzzy Hedgehog

If you’re having difficulties with completing the Fuzzy Hedgehog exercise, or if you had missed a class, the video below will walk you through the complete solution.

Extension Activity: Ribbit the Frog Animation

Student show and tell

71

Reflection

  • What is one new thing you learnt today?

72

Summary

  • Spaces and lines are used in Python to help the programmer better read and understand the code. Python doesn’t care, but other programmers do!
  • print() is used to display text onto the screen
  • The special word input() is used to wait for the ENTER key to be pressed

73

License information

These CS in Schools lessons plans, worksheets, and other materials were created by Toan Huynh and Hugh Williams. They are licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

74

Lesson 2 - Displaying Text on the Screen and Input - Google Slides