Conditionals
Practice Exercises on Conditional Statements
Warmup Exercise
Write a program that reads a password. If the user enters 123 output are you serious?, if the user enters abc output No Way!. Otherwise, print OK!.
Exercise 1
You’d like to write a program that reads a student’s grade and prints the corresponding letter grade. You used DeepSeek, Gemini, ChatGPT, and Claude, and each gave a different piece of code. Which one is correct?
These are just hypothetical pieces of code that were not generated by any GenAI tool.

Solution
- DeepSeek. Prints
A+ABifgrade >= 100and printsABif90 <= grade < 100. - Gemini.Prints an
Fifgrade >= 90. - Claude. Correct!
- ChatGPT. Correct, but is poor style because the
andpart is useless given that we are usingelif.
Exercise 2
What is wrong with the following piece of code? How can we fix it?
if temp > 20:
print("Hot")
elif temp > 25:
print("Warm")
else:
print("Cold")
Solution
This code will never printWarm. To fix it, we need to swap the first condition with the second. Exercise 3
Write a program that reads three integers and prints the maximum.
Which of these solutions do you find more readable?
Exercise 4
💡 NOTE
This exercise is for practice only and should not be understood to mean that you should avoid using operators
andandor. On the contrary, such operators make the code much more readable.
Exercise 4.1
Rewrite the following piece of code without using and, or, or not.
if x == 1 and y == 1:
print("Both are 1")
else:
print("At least one is not 1")
The following two solutions are incorrect, what is wrong with each of them?
Non-Solution 1.
if x == 1:
if y == 1:
print("Both Are 1")
else:
print("At least one is not 1")
Explanation
Nothing will be printed ifx !=1 1. Non-Solution 2.
if x == 1:
if y == 1:
print("Both Are 1")
else:
print("At least one is not 1")
Explanation
Nothing will be printed ifx == 1 and y != 1. While this code is correct, it is not as clean as the first solution we proposed above.
Exercise 4.2
Rewrite the following piece of code so that they do not use and, or, or not.
if x == 1 or y == 1:
print("At least one is 1")
else:
print("Oops!")
Exercise 5
Write a program that reads two integers, x and y, and an operator: +, -, *, or /. The program should print out the result of the operation or output an error message if the oepration is not valid.