~ Creating simple output in Python - printing to the screen


1 - Printing Output in Python

Ouptut (Printing) in Python

Programs are typically made up of three parts: Input - Processing and Output. Output is what comes OUT of a program! That is what we are going to be looking at in this series of tasks. In Python, we produce output to the screen by using the print command. Printing to the screen refers to producing output, not physically printing to a printer! Watch the video below to learn more about printing and output, and then attempt all the tasks. Once you've attempted the tasks, you should be able to do the final challenge which involves putting all your skills together to write some code for yourself.

Download Python 3 here:



Task 1

1.First, run the code to see how it works!. Then fix the indentation error so that the print statement on the wrong indent level works.

2. Add two more sentences by using the print command. This is sentence 3 and This is sentence 4.

3. Add two whole line spaces between sentence 3 and 4 - note the use of the print("") command to do this.

4 At the very start of the program, print the words "Hello World" to the screen.

Code to copy and paste

def main():
  print("This is sentence 1")
  print("")
  print("This is sentence 2")
    print("this is a sentence that will not print") 
main()

..or try it online

Note:use the left hand menu to RESET the trinket if necessary

Task 2

1.Run the code below and see how it works. Note how the variables firstname and surname are overwritten later on in the program with a new firstname and surname

2. Add spaces so that Hello Mr Joe Bloggs prints out properly (and not with each word right next to the other)

3. Add a variable for movie, (e.g. movie="Shrek", and print out _____, is a nice movie!

4 Create variables for colour and food, and put in your own favourite coour and food as values. Print these out in the sentence: So your favourite coour and food are: ____ and _____

Code

import time #this just allows us to work with time functions like sleep
def main():
  firstname="Mister"
  surname="Moose"
  print("Hello"+firstname+surname)
  time.sleep(1.1) #this gives a time delay
  print("Hello "+firstname+ " "+surname)
  time.sleep(0.9)
  firstname="Joe"
  surname="Bloggs"
  print("Hello"+firstname+surname)
  job="Coder" 
  time.sleep(0.9)
  print("Being a "+job+" is so cool")

main()

Task 3

1.Run the code below and see how it works. You'll notice it has a main menu created with lots of text, and this is done inside a primt command with triple speech marks =""" closed with """>

2. Add one more command (before the quit command) in the main menu: >> Press S to Save Scores.

3. In the playgame def function, add some instructions, using the triple quote print command, for the game tic tac toe. eg. "To play Tic Tac Toe, simple enter an x or an o..."

Code

#If you want to write lots of text you can use triple speech marks

def main():
  print("""
  ================MAIN MENU================
  Press P to Play Game
  Press V to View Scores
  Press Q to Quit
  
  
  """)
  playgame()
  
def playgame():
  print("===You are now in the game menu====")
  print("Welcome")

main()

Task 4

1.Run the code below and see how it works. We are basically just creating a very simple menu screen for facebook

2. Create your own menu screen for INSTAGRAM. It should say Welcome to Istagram. 1. Add a Picture 2. Delete a Picture 3. Quit Instagram

Code

#If you want to write lots of text you can use triple speech marks

def facebook():
  print("")
  print("****      Welcome to Facebook    *****")
  print("""
        Press 1. to Register
        Press 2. to Login
        Press 3. to Signout
  
  """)


facebook()

Task 5

1.Run the code below and see how it works. It presents a facebook menu, asks the user to press enter, and then presents a profile menu. I know it doesn't look pretty, but remember you are just focusing on the backend logic and not the front end design! Note how we are 'passing' the variable 'name' to the profile function. This is clever, as it lets the name be used in that function as well!

2. Add a sub menu in the profile menu: 1. Search for other friends 2. Post a message 3. Post a Picture 4. Sign out. Add a little personal message at the end to say: ___(name), have a good day!

Code

#Bringing all our printing skills together

def facebook():
  name=input("Enter Name:")
  print("")
  print("Welcome,"+name)
  print("")
  print("****      Welcome to Facebook    *****")
  print("""
        Press 1. to Register
        Press 2. to Login
        Press 3. to Signout
  
  """)
  press_enter=input("Press enter to continue:")
  profile(name)

def profile(name):
  print("=====FACEBOOK PROFILE ===========")
  print("Welcome! "+name+" ..what would you like to do?")

facebook()

Final Challenge

Use your knowledge and skills from the completed tasks. The empty trinket below can be used to complete the following challenge. Show your teacher when complete!

1. Create a Main Menu with three options 1. Login 2. Register. 3. Quit

2. Create a 'talking news' application, that introduces itself as "Welcome, I am the newsbot, and today's news is.....". The newsbot should then tell you three items of current news (use BBC, CNN or SKY news to help you). Each item of news should come in with a time delay. It should end with a goodbye message of some sort.

Answer #1

Answer #2

Answers

Coming soon!