> Source URL: /demos/cs1-path/labs/lab-02.spec
---
lab_number: 2
title: Making Decisions
outcomes: [2, 6]
difficulty: beginner
prerequisites: [lab-01]
estimated_time: 60-90 minutes
---

# Lab 2: Making Decisions

## Overview

Programs become powerful when they can make decisions. In this lab, you'll learn how to use conditionals (`if`, `elif`, `else`) to make your programs respond differently based on different situations.

## Learning Objectives

By completing this lab, you will be able to:

- [ ] Write boolean expressions that evaluate to True or False
- [ ] Use comparison operators (`==`, `!=`, `<`, `>`, `<=`, `>=`)
- [ ] Create `if` statements to run code conditionally
- [ ] Use `elif` and `else` to handle multiple conditions
- [ ] Combine conditions with `and`, `or`, and `not`

## Prerequisites

Before starting this lab, you should be comfortable with:

- [Lab 1: Hello Python](lab-01.spec.md) - variables, input/output, basic math

## Instructions

### Part 1: True or False

Python can evaluate statements as either `True` or `False`. These are called **boolean values**.

1. Try these comparisons in Python:

```python
# Comparison operators
print(5 > 3)    # Is 5 greater than 3?
print(5 < 3)    # Is 5 less than 3?
print(5 == 5)   # Is 5 equal to 5?
print(5 != 3)   # Is 5 not equal to 3?
print(5 >= 5)   # Is 5 greater than or equal to 5?
print(5 <= 3)   # Is 5 less than or equal to 3?
```

**Expected output:**

```
True
False
True
True
True
False
```

2. You can store boolean values in variables:

```python
is_raining = True
has_umbrella = False
print("Is it raining?", is_raining)
print("Do I have an umbrella?", has_umbrella)
```

### Part 2: Your First If Statement

An `if` statement runs code only when a condition is `True`.

1. Try this simple example:

```python
temperature = 75

if temperature > 70:
    print("It's warm outside!")
```

**Expected output:**

```
It's warm outside!
```

2. Change the temperature to 65 and run again. What happens? (Nothing prints because the condition is `False`.)

3. The code inside the `if` must be **indented** (4 spaces). This is how Python knows what code belongs to the `if`:

```python
temperature = 75

if temperature > 70:
    print("It's warm outside!")
    print("Maybe wear shorts!")  # This is also inside the if

print("Have a nice day!")  # This always runs (not indented under if)
```

### Part 3: If-Else

Use `else` to run different code when the condition is `False`.

1. Create a program that gives different advice:

```python
temperature = int(input("What's the temperature? "))

if temperature > 70:
    print("It's warm! Wear light clothes.")
else:
    print("It's cool. Bring a jacket.")
```

**Example interactions:**

```
What's the temperature? 80
It's warm! Wear light clothes.
```

```
What's the temperature? 55
It's cool. Bring a jacket.
```

### Part 4: Multiple Conditions with Elif

Sometimes you need to check multiple conditions. Use `elif` (short for "else if").

1. Create a grade calculator:

```python
score = int(input("Enter your score (0-100): "))

if score >= 90:
    grade = "A"
elif score >= 80:
    grade = "B"
elif score >= 70:
    grade = "C"
elif score >= 60:
    grade = "D"
else:
    grade = "F"

print("Your grade is:", grade)
```

**Key insight**: Python checks conditions from top to bottom and stops at the first one that's `True`.

2. Try different scores: 95, 85, 72, 55. Verify the grades are correct.

### Part 5: Combining Conditions

Use `and`, `or`, and `not` to combine conditions.

1. **`and`** - Both conditions must be True:

```python
age = 25
has_license = True

if age >= 16 and has_license:
    print("You can drive!")
else:
    print("You cannot drive yet.")
```

2. **`or`** - At least one condition must be True:

```python
is_weekend = True
is_holiday = False

if is_weekend or is_holiday:
    print("No school today!")
else:
    print("Time for class.")
```

3. **`not`** - Reverses True/False:

```python
is_raining = False

if not is_raining:
    print("No umbrella needed!")
```

### Part 6: Putting It Together

Create a program that recommends activities based on weather conditions:

```python
print("=== Activity Recommender ===")

temperature = int(input("Temperature (°F): "))
is_raining = input("Is it raining? (yes/no): ") == "yes"

if is_raining:
    print("Recommendation: Stay inside and read a book!")
elif temperature > 85:
    print("Recommendation: Go swimming!")
elif temperature > 60:
    print("Recommendation: Go for a walk in the park!")
elif temperature > 40:
    print("Recommendation: Perfect weather for a museum visit!")
else:
    print("Recommendation: Bundle up and make hot cocoa!")
```

Notice the trick on line 5: `input(...) == "yes"` evaluates to `True` or `False`.

## Submission

Create a program called `number_game.py` that:

1. Picks a "secret" number (just hardcode it, like `secret = 7`)
2. Asks the user to guess the number
3. Tells them if their guess is:
   - Too high
   - Too low
   - Correct!
4. If correct, also tell them how far off they were (you can use `abs()` for absolute value)

**Example interaction:**

```
I'm thinking of a number between 1 and 10.
Your guess: 5
Too low! Try again.
Your guess: 8
Too high! Try again.
Your guess: 7
Correct! You got it!
```

Bonus: Add a message if they guess within 1 of the correct answer ("So close!").

Push your `number_game.py` to your GitHub repository.

## Self-Check

Before submitting, verify:

- [ ] Program runs without errors
- [ ] All three cases work (too high, too low, correct)
- [ ] Your code uses meaningful variable names
- [ ] Indentation is consistent (4 spaces)

## Extension Challenges (Optional)

1. **Leap Year Checker**: A year is a leap year if it's divisible by 4, except years divisible by 100 aren't leap years, unless they're also divisible by 400. Write a program that checks if a year is a leap year.

2. **Login System**: Create a simple login that checks both username AND password. Give different error messages for "wrong username" vs "wrong password" vs "both wrong."

## Getting Help

- Review [Lecture 2: Control Flow](../lectures/lecture-02.slides.md)
- Check the course discussion forum
- Attend office hours


---

## Backlinks

The following sources link to this document:

- [Lab 2: Making Decisions](/demos/cs1-path/index.path.llm.md)
- [Lab 2: Making Decisions](/demos/cs1-path/course.outcomes.llm.md)
- [Lab 2](/demos/cs1-path/lectures/lecture-02.slides.llm.md)
- [Lab 2: Making Decisions](/demos/cs1-path/labs/lab-03.spec.llm.md)
- [Lab 2: Making Decisions](/demos/cs1-path/course.schedule.llm.md)
