Conditionals

  • Affect the sequential flow of control by executing different statements based on the value of a Boolean expression. IF (condition) { }

The code in is executed if the Boolean expression condition evaluates to true; no action is taken if condition evaluates to false.

IF (condition) { } ELSE { }

The code in the first is executed if the Boolean expression condition evaluates to true; otherwise, the code in is executed. Example: Calculate the sum of 2 numbers. If the sum is greater than 10, display 10; otherwise, display the sum. num1 = INPUT num2 = INPUT sum = num1 + num2 IF (sum > 10) { DISPLAY (10) } ELSE { DISPLAY (sum) }

Example

  • Add a variable that represents an age.

  • Add an ‘if’ and ‘print’ function that says “You are an adult” if your age is greater than or equal to 18.

  • Make a function that prints “You are a minor” with the else function.


age = input("age: ")
if int(age) >= 18:
    print("Adult")
else:
    print("Minor")

Example

  • Make a variable called ‘is_raining’ and set it to ‘True”.

  • Make an if statement that prints “Bring an umbrella!” if it is true

  • Make an else statement that says “The weather is clear”.


is_raining = False
if is_raining:
    print("Bring a umbrella")
else:
    print("The weather is clear")

Example

  • Make a function to randomize numbers between 0 and 100 to be assigned to variables a and b using random.randint

  • Print the values of the variables

  • Print the relationship of the variables; a is more than, same as, or less than b


import random


a = random.randint(0,100)
b = random.randint(0,100)
print(a, b)

if a > b:
    print( str(a) + " > " + str(b) )
elif a < b: 
    print( str(a) + " < " + str(b) )
else:
    print( str(a) + " = " + str(b) )


Hacks

Create a 5 question quiz following the comments in the #HOMEWOR code cell below.

  1. Provided is question_with_response function and some introductory samples.
  2. A key to the homeworks is to research and come up with each question type.
  3. A key to the program is to evaluate input from user and calculate a result
#HOMEWORK


# The quiz contains questions related to Boolean values, Boolean expressions,
# conditional statements, relational operators, and logical operators.

# Import necessary modules
import getpass  # Module to get the user's name
import sys  # Module to access system-related information

# Function to ask a question and get a response
def question_with_response(prompt):
    # Print the question
    # Get user input as the response
    answer = input(prompt)
    return answer

# Define the number of questions and initialize the correct answers counter
questions = 5
correct = 0

# Personalized greeting message
# Collect the student's name
user_name = question_with_response("Enter your name: ")
print('Hello, ' + user_name + " you are running " + sys.executable)
ready = question_with_response("Are you ready to take a test? (yes/no): ")

# Question 1: Boolean Basics 
# Ask a question about Boolean values and check the response
# Provide feedback based on the correctness of the response

# Question 2: Boolean Expressions
# Ask a question about Boolean expressions and their importance in programming
# Provide feedback based on the correctness of the response

# Question 3: Conditional Statements
# Ask a question about the purpose of conditional (if-else) statements in programming
# Provide feedback based on the correctness of the response

# Question 4: Relational Operators
# Ask a question about common relational operators in programming and provide examples
# Provide feedback based on the correctness of the response

# Question 5: Logical Operators
# Ask a question about the use of logical operators in programming and provide examples
# Provide feedback based on the correctness of the response

# Display the final score
# Calculate the percentage of correct answers and provide feedback
print(user_name + ", you scored " + str(correct) + "/" + str(questions))
Hello, John running /opt/homebrew/opt/python@3.11/bin/python3.11
John, you scored 0/5