Quiz:Conditional Execution
Quiz:Conditional Execution
if x == 5 :
print('Is 5')
print('Is Still 5')
print('Third 5')
Answer: Depending on the value of x, either all three of the print statements will
execute or none of the statements will execute
// If x is equal to 5, all the statements are run because they are indented
below the comparison.
4 - When you have multiple lines in an if block, how do you indicate the end of the
if block?
Answer: You de-indent the next line past the if block to the same level of indent
as the original if statement
// Se answer 1
if x == 6 :
print('Is 6')
print('Is Still 6')
print('Third 6')
It looks perfect but Python is giving you an 'Indentation Error' on the second
print statement. What is the most likely reason?
//This is very tricky so let's go step by step.
In order to make humans feel inadequate, Python randomly emits 'Indentation Errors'
on perfectly good code -
after about an hour the error will just go away without any changes to your program
// Well.... this is ridiculous
Python has reached its limit on the largest Python program that can be run
// As long you have the memory and the CPU power, your program can be as large
as you want.
7 - What is the Python reserved word that we use in two-way if tests to indicate
the block of code that is
to be executed if the logical test is false?
Answer: else
//The curly brackets are not words and the other two are not in the list of
reserved words.
x = 0
if x < 2 :
print('Small')
elif x < 10 :
print('Medium')
else :
print('LARGE')
print('All done')
if x < 2 :
print('Below 2')
elif x >= 2 :
print('Two or more')
else :
print('Something else')
Answer: This code will never print 'Something else' regardless of the value for 'x'
// The code evaluates a number less than 2 or "from" 2 to up, there is no room
for another number.
10 - In the following code (numbers added) - which will be the last line to execute
successfully?
Answwer: 1
// When trying to parse "astr" to "int", that contains the string 'Hello Bob',
you get an error
you cannot parse strings into numbers
Answer: -1
//You get an error whe try to parse a "string" to "int" and then the "except"
is executed.