Output Statements
Now that we’ve mastered getting inputs, let’s talk about presenting the results. The OUTPUT statement in pseudo-code becomes print() in Python. Let’s add some sparkle!
Basic OUTPUT
Example:
In pseudo-code:
OUTPUT "Hello, World!"
Translated to Python:
print("Hello, World!")
Result:
Hello, World!
Formatting Outputs
Concatenation
In pseudo-code:
OUTPUT "Hello, " + Name
Translated to Python:
Name = "Alice"
print("Hello, " + Name)
Formatted Strings (f-strings)
Let’s display age and name together:
DECLARE Age : INTEGER
DECLARE Name : STRING
OUTPUT "Hi " + Name + ", you are " + Age + " years old."
Translated to Python:
Name = "Alice"
Age = 20
print(f"Hi {Name}, you are {Age} years old.")
Outputting Arrays
1D Arrays
In pseudo-code:
DECLARE Colours : ARRAY[1:5] OF STRING
OUTPUT "Your favourite colours are:"
FOR Counter ← 1 TO 5
OUTPUT Colours[Counter]
NEXT Counter
Translated to Python:
Colours = ["Red", "Blue", "Green", "Yellow", "Purple"]
print("Your favourite colours are:")
for Colour in Colours:
print(Colour)
2D Arrays
In pseudo-code:
DECLARE Grades : ARRAY[1:2, 1:3] OF INTEGER
OUTPUT "The grades are:"
FOR Row ← 1 TO 2
FOR Col ← 1 TO 3
OUTPUT Grades[Row, Col]
NEXT Col
NEXT Row
Translated to Python:
Grades = [[85, 90, 88], [92, 80, 85]]
print("The grades are:")
for Row in Grades:
print(" ".join(map(str, Row))) # Joins row elements with a space
Let’s Make It Fun!
Name = input("What’s your name? ")
Snack = input("What’s your favourite snack? ")
Age = int(input("How many years have you been enjoying snacks? "))
print(f"Hi {Name}! You’ve been enjoying {Snack} for {Age} years!")
Example output:
What’s your name? Alice
What’s your favourite snack? Chocolate
How many years have you been enjoying snacks? 10
Hi Alice! You’ve been enjoying Chocolate for 10 years!
Recap
Input and output aren’t just about writing code—they’re about creating an engaging user experience. Whether you’re asking for a user’s name or printing an array of grades, you’re building a connection between the program and the user.
Remember: Always test your inputs (and watch out for users like your cheeky mate who enters a text string when you’re expecting a number)!