We know that for an if statement to be followed we need the condition to be
true. If we had two variables, A and B and one of those was true and our
condition was A or B then we would get true. If the condition was changed to
be A and B, then the result of the condition would be false.
Can the pupil leave?
The following table gives a condition to the pupil given by the class teacher. Complete the table showing whether the pupils get to leave the classroom based on the teacherโs conditions.
| Teacher says before pupils can leave they must | Pupils do | Can pupils leave? |
|---|---|---|
| "Put all chairs under desks and all notes in bag" | Pupils put all chairs under desk | |
| "Complete task 1 or attempt programming worksheet" | Half the class do task 1 and finish it and the other half start programming worksheet | |
| "Score 50 marks in an online test or pass your class test" | All pupils passed the class test but only half managed to get 50 marks the online test |
Truth Tables
-
1
Complete the following table
A B A and B A or B True True True True True False False False True True False False Test your answers by writing a program to do that.
-
2
Complete the following table
Variable 1 Variable 2 Condition Result A = 10 B = 50 A < B A = 50 B = 20 B < 50 C = 31 A = 31 C == A A = 30 B = 25 A == B or B > 30 A = False B = True A and B A = False B = True A or B Test your answers by writing a program to do that.
Python challenges
-
1
Write a multiple-choice quiz program that makes use of if statements. The quiz can be on any topic you like related to school, for instance you could do a quiz on physics or history. This program will ask the user to type a number 1, 2, 3 or 4 to give the answer and will look something like the following:
The steps for the program are as follows:
-
Set a total points variable to 0 (
total_points = 0) -
Display the question (
print("What is the capital of Germany?")) -
Display the options (
print("1 = Paris, 2 = Berlin, 3 = Sofia, 4 = Madrid")) -
Ask the user for their answer and store it in a variable called
user_answer (
user_answer = input()). Note that the code solution here is not complete, something is missing! -
Now check that the number of the answer matches the number given by the
user. (
if user_answer == 2:). -
Finally, if they give the correct answer, give them one point.
If they give the wrong answer, donโt give them a point. (
total_points = total_points + 1) - Now move onto the next question and repeat from step 2 onwards.
-
When you have programmed all of the questions, display the number of points. (
print(total_points))
Add your code below:
-
Set a total points variable to 0 (
-
2
Write a Python program that takes a student's score as input and classifies it into a letter grade based on the following scale:
- A : 70%+
- B : 60%+
- C : 50%+
- D : 40%+
Steps:
- Use an input statement to get the student's score.
- Use if statements to determine the letter grade.
- Print the corresponding letter grade.
-
3
Write a Python program that functions as a simple calculator. The program should:
- Ask the user to input two numbers.
- Ask the user to choose an operation (addition, subtraction, multiplication, division).
- Perform the chosen operation and display the result.