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!.

Solution
password = input("Enter a password: ")
if password == '123':
    print("Are you serious?")
elif password == 'abcd':
    print("No Way!")
else:
    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.


Comic: My Password is Password123

Solution
  • DeepSeek. Prints A+AB if grade >= 100 and prints AB if 90 <= grade < 100.
  • Gemini.Prints an F if grade >= 90.
  • Claude. Correct!
  • ChatGPT. Correct, but is poor style because the and part is useless given that we are using elif.

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 print Warm. 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.

Solution 1
x = int(input())
y = int(input())
z = int(input())

if x >= y and x >= z:
    print(x)
elif y >= x and y >= z:
    print(y)
else:
    print(z)

                
Solution 2
x = int(input())
y = int(input())
z = int(input())

m = x       # assume x is the maximum

if y > m:   # if y is greater than the current maximum
    m = y   # make y the new maximum

if z > m:   # if z is greater than the current maximum
    m = z   # make z the new maximum

print(m)    # print the maximum
                
Solution 3
x = int(input())
y = int(input())
z = int(input())

if x >= y:
    if x >= z:
        print(x)
    else:
        print(z)
elif y >= z:
    print(y)
else:
    print(z)
                

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 and and or. 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")
Solution 1
if x != 1:
    print("At least one is not 1")
elif y != 1:
    print("At least one is not 1")
else:
    print("Both are 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 if x !=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 if x == 1 and y != 1.


Corrected Code
if x == 1:
    if y == 1:
        print("Both Are 1")
    else:
        print("At least one is not 1")
else:
    print("At least one is not 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!")
Solution
if x == 1:
    print("At least one is 1")
elif 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.

Solution
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 
# 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.   #
#                                                             #
# --------------------- EXAMPLE RUN ------------------------- #
# Enter x: 1                                                  #
# Enter y: 3                                                  #
# Enter opeartion: +                                          #
# Result = 4                                                  #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 

x = int(input("Enter x: "))
y = int(input("Enter y: "))
op = input("Enter the operator: ")

if op == '+':
    print("Result = ", x + y)
elif op == '-':
    print("Result = ", x - y)
elif op == '*':
    print("Result = ", x * y)
elif op == '/':
    if y == 0:
        print("ERROR: CAN'T DIVID BY ZERO!")
    else:
        print("Result = ", x / y)
else:
    print("ERROR: INVALID OPERATION!")
                

© Ibrahim Albluwi. · Last updated: