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.

1
Q

What does conditional statement mean?

A

“Conditional statements” help a computer answer “yes” or “no” questions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are the things that an if statement comprises of?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How do we get out of our block of code for functions and if statement?

A

We have to press enter twice to get out of our block of code.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

The if is highlighted in which colour for if statements?

A

In red

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

The condition part of the if statement is highlighted in what colour?

A

Blue

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What colour is the colon highlighted in for the if statement?

A

Orange

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

For the if statement, what happens if the value does not fit into the condition?

A

Nothing will print out.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

The condition equals to is represented by which symbol?

A

==

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

When is one equal sign used?

A

When we want to assign a value to a variable.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the definition of this symbol != ?

A

Not equal to

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the definition of this symbol < ?

A

Less than

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the definition of this symbol > ?

A

Greater than

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the definition of this symbol <= ?

A

Less than or equal to

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is the definition of this symbol >= ?

A

Greater than or equal to

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

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?

A

>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

To do something different when the answer is “no” for if statements, we use _____.

A

the if-then-else statement

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What is the difference between the if part and the else part?

A

If part contains the conditions while the else part does not.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

How do you enter the else part?

A

To enter the else part, you will need to press backspace or delete to remove the white space and start a new block.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

What must you take note when you are coding your if and else statement?

A

Do not press enter

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q
A

This is a good time to start using a separate document for writing your code instead of typing it into IDLE.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

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?

A

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)

22
Q

What does the elif statement do?

A

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.

23
Q

Do you remember what the int is for?

A

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.

24
Q

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.

A

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”)

25
Q

Can you have more than one elif in an if elif else statement?

A

Yes

26
Q

When you compare two values in Python, the result is a ____, either ____ or ___.

A

The result is a Boolean, either True or False.

27
Q

What would be the output for this?
print(True==“True”)
print(False==“False”)

A

False
False

Note that the Boolean value True is different than the string “True”. Python stores these as two different values or data types.

28
Q

Can you use Boolean True or False in your if-else statements to compare?

A

yes

29
Q

In Boolean logic, the OR-operator means that if_____ condition is ____, the resulting expression is true.

A

In Boolean logic, the OR-operator means that if either condition is true, the resulting expression is true.

Here’s an example:
If I clean my room OR I finish my homework, then I can go to the movies with my friends. This means that you only need to do one of these tasks, either clean your room or finish your homework. Then, you are able to go to the movies with your friends.

30
Q

In Boolean logic, the AND-operator means that if ___ conditions are ___, the resulting expression is true.

A

If both conditions are true, then the resulting expression is true.

Here’s an example:
If I clean my room AND I finish my homework, then I can go to the movies with my friends. This means that you must do both of these tasks, clean your room and finish your homework. Only after both are completed are you able to go to the movies with friends.

30
Q

What will happen when we use the word “or” for if statement?

A

Using “or” will make the if statement run if any of the conditions are true.

31
Q

What is the output of the following program?

The first letter of each line of code should not be capitalise but for my convenience i will type so, thus it is not an error.

Raining = False
If raining:
Print(“Bring an umbrella today!”)
Else:
Print(“The skies are clear!”)

Choose one option:
A: Bring an umbrella today!
B: The skies are clear!
C: an error occurs
D: Bring an umbrella today! The skies are clear!

A

B
You can either write
If raining == True:
Or If raining:

31
Q

how do we put multiple conditions together?

A

We can use the keywords “or” and “and”. We need to learn how to use multiple conditions in one if statement to make efficient code.

32
Q

What will happen when we use the word “and” for if statement?

A

“And” will make it run if all of the conditions are true.

33
Q

Can we put an if statement inside another if statement?

A

Yes

34
Q

cost = 30
if cost < 30:
print(“I can afford it”)
else:
print(“I can’t afford it”)

What is wrong with this and why?

A

It is an indentation error.

Indentation errors
Just like functions, if-else statements also require you to indent the block correctly and with consistent whitespace.

Try typing this code into IDLE and running it. What happens? Python throws a SyntaxError: expected an indented block. Can you figure out why? We forgot to indent the first print statement in the body of the if statement. Similarly, you will also get a syntax error if you’re not consistent with the number of spaces (whitespace) before each indented line. Here is the corrected code:

cost = 30
if cost < 30:
print(“I can afford it”)
else:
print(“I can’t afford it”)
*TIP: Remember to indent the code block in the body of if, elif and else by using consistent whitespace. Also, make sure any elif or else blocks are indented the same way as the if block.

35
Q

cost = 30
if cost < 30:
print(“I will buy it”)
elif cost = 30:
print(“I can’t afford it”)

What is wrong with this code and why?

A

It is an error with equality operator (==).
Another common error when working with conditionals is mixing up the assignment operator (=) with the equality operator (==). Remember that “==” is for checking if two values equal each other and “=” is to assign a value to a variable.

When we run this code, Python throw a SyntaxError: invalid syntax. Take a closer look at the 4th line of code:

elif cost = 30:
This line uses the assignment operator (=) instead of comparison operator (==), and Python doesn’t understand what to do. The correct way to write this code is:

cost = 30
if cost < 30:
print(“I will buy it”)
elif cost == 30:
print(“I can’t afford it”)
*TIP: Make sure to use the comparison operator (==) and not the assignment (=) operator to compare two values in Python.

36
Q

cost = 30
if cost < 30:
print(“I will buy it”)
else cost >= 30:
print(“I can’t afford it”)

What is wrong with this code and why?

A

It is known as an error with else.

Try running the code in IDLE and you get a SyntaxError: invalid syntax. Remember that in Python, the else statement takes no conditions and executes only when all other conditions are false. If we have multiple conditions to check, we should use elif statements. Here is the corrected code:

cost = 30
if cost < 30:
print(“I will buy it”)
else: #We can assume that cost is >= 30 here
print(“I can’t afford it”)
*TIP: Remember that in Python, an if statement can only have one else statement and that the else block cannot include a condition.

37
Q

cost = 30
if cost < 30
print(“I will buy it”)
elif cost == 30
print(“I can’t afford it”)

What is wrong with this code and why?

A

There is an error as the person forgot to add a colon (:)
An if statement always needs a colon at the end of the line.

The above program will not run and throws a SyntaxError: invalid syntax because we are missing colon ( : ) at the end of both the if and elif statements. Here is the corrected code:

cost = 30
if cost < 30:
print(“I will buy it”)
elif cost == 30:
print(“I can’t afford it”)
*TIP: Always remember to put a colon (:) after if, elif and else statements.

38
Q

Reflect upon what you learnt for unit 6. What are some key points to take note and a summary of a few points to guide you when writing your program?

A

Summary
Each code block in if, elif and else statements must be indented and consistent with the number of whitespace.
The assignment operator should not be confused with equality operator (==). Make sure your equality test condition uses the equality conditional operator (==) and not an assignment operator (=).
Don’t add a condition for an else block otherwise Python will throw an error.
Make sure to add the colon (:) at the end of if, elif and else statements.

39
Q

Which of the following Python keywords can be used to combine conditions?

*Choose all that apply.

Question 1Select one or more:

a.
def

b.
and

c.
in

d.
or

e.
as

f.
del

A

b and d

40
Q

In the program below, which of the lines of code contain an error?

*Choose all that apply.

item = ‘Pen’
if item = ‘Pencil’:
cost = 0.99
else if item == ‘Pen’:
cost = 1.99
else
cost = 0.0

print(‘Item {} costs {}’.format(item, cost))
Question 2Select one or more:

a.
item = ‘Pen’

b.
if item = ‘Pencil’:

c.
cost = 0.99

d.
else if item == ‘Pen’:

e.
cost = 1.99

f.
else

g.
cost = 0.0

h.
print(‘Item {} costs {}’.format(item, cost))

A

b,d and f.

This is because the line of code for b should have a double equal sign instead of just having one equal sign.

For d, it should not be else if but elif

For f, there should be this symbol :

41
Q

What is the output of the following program?

def stage_of_life(age):
age = int(age)
if age >= 21:
return “adult”
elif age >= 18 and age < 21:
return “young adult”
elif age >= 12 and age < 18:
return “teenager”
elif age >= 5 and age < 12:
return “gradeschooler”
elif age >= 3 and age < 5:
return “preschooler”
else:
return “baby or toddler”

print(stage_of_life(12))
Question 3Select one:

a.
teenager

b.
gradeschooler

c.
tween

d.
young adult

A

a because it fulfills that condition

42
Q

a.

Which of the following symbols is used to define an “equal-to” condition?

Question 4Select one:

c.
and

A

d

43
Q

What is the output for the following code snippet?

score = 90

if score < 60:
    print('Grade: F')
elif score > 60:
    print('Grade: D')
elif score > 70:
    print('Grade: C')
elif score > 80:
    print('Grade: B')
elif score > 90:
    print('Grade: A')
elif score > 98:
    print('Grade: A+')
else:
    print('Invalid score') Question 5Select one:

a.
Grade: D

b.
Grade: A

c.
Grade: B

d.
Invalid score

A

Option A

The reason why it is option a is because the program looks at the first line of code, if score<60: but it does not fulfil the condition, so it moves to the next one. elif score>60: and this is true so print(‘Grade:D’)

The program does not check any further conditions after the first True condition is found.

44
Q

What is the output of the following program?

course_name = ‘Python’

if course_name == ‘Python’:
print(‘I am taking a Python course now.’)
print(‘{} is great!’.format(course_name))

elif course_name == ‘Java’:
print(‘I will take Java next.’)
print(‘{} is a high-level language.’.format(course_name))

print(‘Computer programming is fun.’)
Question 6Select one:

a.
I am taking a Python course now.
Python is great!
Computer programming is fun.

b.
I will take Java next.
Java is a high-level language.
Computer programming is fun.

c.
I am taking Python course now.
Python is great!
Python is a high-level language.
Computer programming is fun.

d.
I am taking Python course now.
Python is great!
Java is a high-level language.
Computer programming is fun.

A

Option A. the last line of print statement applies regardless of the course name.

45
Q

What will be the output of the following program?

def gpa_to_letter(gpa):
gpa = float(gpa)
if gpa == 4.0:
return ‘A’
elif gpa == 4.0:
return ‘A+’
elif gpa == 3.7:
return ‘A-‘
else:
return ‘B or below’

print(gpa_to_letter(4.0))
Question 7Select one:

a.
B or below

b.
A+

c.
A

d.
An error message is displayed.

A

c.
The program does not check any further conditions after the first True condition is found.
so the line that fulfil the conditions first will be printed

46
Q
A
47
Q

What is the output of the following code snippet?

next_player = ‘Jim’
if next_player == ‘Jim’:
print(‘Jim played his turn’)
next_player = ‘Shelly’
elif next_player == ‘Shelly’:
print(‘Shelly played her turn’)
next_player = ‘Ryan’
else:
print(‘Ryan played his turn’)

print(‘{} was the last player’.format(next_player))
Question 8Select one:

a.
Jim played his turn
Shelly played her turn
Ryan played his turn
Ryan was the last player

b.
Jim played his turn
Shelly played her turn
Shelly was the last player

c.
Jim played his turn
Jim was the last player

d.
Jim played his turn
Shelly was the last player

A

Explanation:
Initialization: The variable next_player is initialized to ‘Jim’.
First Condition: The code checks if next_player == ‘Jim’, which is True.
It executes the first block:
print(‘Jim played his turn’) outputs: Jim played his turn
Then it sets next_player to ‘Shelly’.
Second Condition: The next elif checks if next_player == ‘Shelly’, but this condition is not checked because the previous if was True and the code does not proceed to the elif.
Final Output: The code then reaches the final print statement:
print(‘{} was the last player’.format(next_player)) outputs: Shelly was the last player because next_player was set to ‘Shelly’.
Correct Answer:
d. Jim played his turn
Shelly was the last player

48
Q

An if statement evaluates a(n) _________ statement.

Question 9Select one:

a.
parameter

b.
conditional

c.
error

d.
argument

A

Option b is the correct answer.

Explanation:
An if statement in programming evaluates a conditional statement, which determines whether the code inside the if block should execute based on whether the condition is True or False.

49
Q

When combining two or more conditions that all must be true, we use the keyword…

Question 10Select one:

a.
not

b.
or

c.
and

d.
with

A

c