Preview

02 - Printing, Comments, Indentation

 1. Programs are typically made up of three parts: Input, Processing and …..
Note: Do feel free to watch the following video that goes over the fundamentals of printing in Python

  printer paper

  coded comments

  dynamic

  output

 2. Printing in Python is one of the simplest things you can do. What will the following command output to the screen?
print("Hello World")

  Hello

  print("Hello World")

  Hello World

  Error

 3. When we talk about 'printing' something to the screen we mean …

  …that binary signals are sent directly to the CPU

  None of the above

  …that something will be printed directly to the printer

  …that something will be output to the screen (e.g text)

 4. Identation matters in Python. Which of the following statements is true?

  Indentation is not necessary in Python - it is only used to make it easier for the programmer to visually see the code

  Every single line in python must be indented by at least two spaces

  Python only uses indentation to specify when a loop or an if statement is being used

  A four space (or tab) indent is typical for Python. Indentation is essential and it provides structure and meaning to the code

 5. What is wrong with the following code? Try it yourself if you are unsure.
def main():
  print("This is sentence 1")
  print("")
  print("This is sentence 2")
    print("this is a sentence that will not print unless the indent is right")
  
main()

  There is nothing wrong with the code

  The fourth print statement is indented too far to the right

  Everything, including the def main(), should be on the same line as the print statements.

  The print("") will cause an error - it is not a valid command

 6. Printing a single line of text or word is done like this: print("word"), but how would you print several lines?
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()

  It cannot be done

  You would have to use a comment

  You can create the print statement inside a function

  You can use triple speech marks: e.g. print("""several lines here """)

 7. On what lines have comments been used?
#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)
#This is another function called 'profile'
def profile(name):
  print("=====FACEBOOK PROFILE ===========")
  print("Welcome! "+name+" ..what would you like to do?")

facebook()

  There are no comments

  2 and 5

  3 and 18

  1 and 17

 8. What is line 5 likely to print to the screen?
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()

  MisterMoose

  HelloMisterMoose

  None of the above

  print(Hello+Mister+Moose)

 9. What will the following code print to the screen?
x=1
print(x)

  print(x)

  x

  1

  Error

 10. Line 3 has an error. What is causing it?
def main():
  print("Hello there-  please press enter")
  print(name)


main()

  This line is a comment so it will not run

  name' is a reserved word so Python can never print it

  The indentation is wrong on this line

  You are trying to print a variable 'name' which doesn't exist yet

 11. What is the output of the following lines of code?
x=1
y=2
z=3
a=1
print(x+1)
print(2)
print("2"+"y")

  x1,2,2y

  2,2,4

  2,2,2y

  1,2,4

 12. What is the output of the following line of code?
a=1
print(a+1)

  a1

  a

  1

  2

 13. What will this code output?
x=0
print("x")

  1

  Error

  0

  x

 14. In some languages, indentation is used only to help make the code look pretty. In Python, it is required for indicating what block of code a statement belongs to.

  FALSE

  TRUE

 15. Read the following excerpt on the use of comments and decide whether it is true or false.
In computer programming, a comment is a programmer-readable explanation 
or annotation in the source code of a computer program. 
They are added with the purpose of making the source code easier for 
humans to understand, and are generally ignored by compilers and interpreters.

  TRUE

  FALSE