Unit 6 Flashcards
In this unit, you will learn how to use conditional statements which are commands for handling decision points in your program. By the end of this unit, you will be able to: Use one or more conditional statements to control a program’s flow. Evaluate basic Boolean logic. (51 cards)
What does conditional statement mean?
“Conditional statements” help a computer answer “yes” or “no” questions.
What are the things that an if statement comprises of?
An if statement has the keyword (if), a condition, and a colon. You will have a block of code that’s indented so that the computer knows that it belongs to the if statement.
How do we get out of our block of code for functions and if statement?
We have to press enter twice to get out of our block of code.
The if is highlighted in which colour for if statements?
In red
The condition part of the if statement is highlighted in what colour?
Blue
What colour is the colon highlighted in for the if statement?
Orange
For the if statement, what happens if the value does not fit into the condition?
Nothing will print out.
The condition equals to is represented by which symbol?
==
When is one equal sign used?
When we want to assign a value to a variable.
What is the definition of this symbol != ?
Not equal to
What is the definition of this symbol < ?
Less than
What is the definition of this symbol > ?
Greater than
What is the definition of this symbol <= ?
Less than or equal to
What is the definition of this symbol >= ?
Greater than or equal to
If I have at least $50, then I can buy the new video game.
if my_money ___49:
Print(“I can buy the new video game”)
What symbol should be filled in the blank?
>
To do something different when the answer is “no” for if statements, we use _____.
the if-then-else statement
What is the difference between the if part and the else part?
If part contains the conditions while the else part does not.
How do you enter the else part?
To enter the else part, you will need to press backspace or delete to remove the white space and start a new block.
What must you take note when you are coding your if and else statement?
Do not press enter
This is a good time to start using a separate document for writing your code instead of typing it into IDLE.
How do you code a program that has the if, else statement and has an option to choose whether to have a red or blue roof?
Ignore all the capitalise letters at the beginning of the sentence since it is not suppose to be in that case in python.
Import turtle
Roof=turtle.Pen()
Blue_roof = ‘no’
If blue_roof == ‘yes’:
Roof.color(‘blue’)
Else:
Roof.color(‘red’)
Additionally, we put that roof code after the if-then-else statement.
Roof.forward(60)
Roof.left(120)
Roof.forward (60)
Roof.left(120)
Roof.forward(60)
Roof.color(“brown”)
Roof.right(-120)
Roof.forward(60)
Roof.right (90)
Roof.forward (60)
Roof.right (90)
Roof.forward (60)
Roof.right (90)
Roof.forward (60)
Roof.right(90)
What does the elif statement do?
It will allow you to ask multiple yes/no questions about the same thing so that you don’t have to repeat the same condition over and over again.
We could ask “ is it less than $20? Is equal to $20? Is it neither?”
Elif is short for else-if
It works very similarly to the if statement but it has to come after an if statement instead of being able to be used by itself.
Do you remember what the int is for?
When we use the input() function, it gives us a string, but if we want to be able to compare numbers, we have to convert it into an integer.
You are playing a game with the following score and player levels:
0-75 points-You are a Novice
76-250 points- You are Advanced
Over 250 points- You are an expert
Check the user’s current score and print out the corresponding player level.
Set the score
Please ignore all the capitalise first letter here because it should not be the case in Python
Score = 250
#Check for novice level
If score<=75:
Print(“Novice”)
#Check for Advanced level
Elif score<=250:
Print(“Advanced”)
#Check for expert level
Else:
Print(“Expert”)