= 8 # Rate the flat from 0-10
flat_rating
if flat_rating >= 7:
print("This is a good apartment!")
This is a good apartment!
Programming: Everyday Decision-Making Algorithms
Imagine you’re searching for a new apartment. You view a flat - should you take it or keep looking for something better? This is another classic optimal stopping problem! Just like in dating or hiring, you need to decide when to stop searching.
In this tutorial, we’ll learn how computers make decisions using if
and else
statements. These are like the “if this, then that” decisions we make every day!
An if
statement is like asking a yes/no question and doing something based on the answer. For example:
= 8 # Rate the flat from 0-10
flat_rating
if flat_rating >= 7:
print("This is a good apartment!")
This is a good apartment!
The structure is:
if
:
Think of if
statements like everyday decisions:
Computer if
statements work exactly the same way! They check a condition and then do something based on that check.
Common Mistakes with If Statements:
:
after the condition=
(assignment) instead of ==
(comparison)True
or False
Create an if
statement that prints “Perfect flat!” if the flat_rating
is 10.
= 10
flat_rating # YOUR CODE BELOW
# Test your answer yourself - the cell should print "Perfect flat!" if executed correctly
But what if we want to do something different when the condition is False? That’s where else
comes in:
= 4 # Not a great flat!
flat_rating
if flat_rating >= 7:
print("Apply for this flat!")
else:
print("Keep searching!")
Keep searching!
The structure is:
if
statement with its conditionelse
and a colon :
Think of if-else
like complete either/or decisions:
The else
statement is our backup plan!
Write an if-else statement that sets decision
to “Apply now” if flat_rating
is at least 7, and “Keep searching” otherwise.
= 6
flat_rating = ""
decision # YOUR CODE BELOW
# Test your answer
assert decision == "Keep searching", "The decision should be 'Keep searching' as the flat rating is less than 7"
print(f"Decision: {decision} as the flat rating is {flat_rating}")
Writing Better If-Else Statements:
Sometimes we need more than two options. That’s where elif
comes in:
= 8
flat_rating
if flat_rating >= 9:
print("Amazing flat - apply immediately!")
elif flat_rating >= 7:
print("Good flat - consider applying")
elif flat_rating >= 5:
print("Mediocre flat - keep it as backup")
else:
print("Poor flat - definitely keep looking")
Good flat - consider applying
The structure adds:
elif
conditions between if
and else
Think of elif
like multiple-choice decisions:
It’s like a flowchart where only one path can be taken!
Important Elif Rules:
elif
must come after if
and before else
elif
blocks as you needCreate a variable flat_category
that is:
= 8
flat_rating = ""
flat_category # YOUR CODE BELOW
# Test your answer
assert flat_category == "Premium", "The flat category should be 'Premium' as the flat rating is 8"
print(f"Flat Category: {flat_category} as the flat rating is {flat_rating}")
In real life, we often need to check multiple conditions at once. We can combine conditions using and
and or
:
= 8
flat_rating = 3
weeks_searching = 4
max_search_time
if (flat_rating >= 7) and (weeks_searching < max_search_time):
print("Take this flat - it's good enough and we still have time!")
elif (flat_rating >= 9) or (weeks_searching >= max_search_time):
print("Take this flat - either it's perfect or we're out of time!")
else:
print("Keep looking!")
Take this flat - it's good enough and we still have time!
Tips for Complex Conditions:
Create a variable should_apply
that is True if:
= 6
flat_rating = 4
weeks_searching = False
should_apply # YOUR CODE BELOW
# Test your answer
assert should_apply == True, "The flat rating is 6 and we've been searching for 4 weeks, so we should apply"
print(f"Should we apply? {should_apply} as the flat rating is {flat_rating} and we've been searching for {weeks_searching} weeks")
Remember the optimal stopping rule? Let’s apply it to apartment hunting:
= 6
flats_seen = 15
total_viewings = 8
current_rating = 7
best_rating_so_far
# Calculate if we've passed 37% of viewings
= flats_seen >= (total_viewings * 0.37)
threshold_passed
if not threshold_passed:
print("Keep looking - still in observation phase")
elif current_rating > best_rating_so_far:
print("Apply for this flat!")
else:
print("Keep looking - waiting for better than our best")
Apply for this flat!
Adjust the code below to change the variable make_application
to True if:
= 6 # We've seen 6 flats
flats_seen = 15 # We plan to view 15 flats in total
total_viewings = 9
current_rating = 8
best_rating_so_far = False
make_application # YOUR CODE BELOW
# Test your answer
assert make_application == True, "The flat rating is 9 and we've been searching for 4 weeks, so we should apply"
print(f"Should we apply for this flat? {make_application}")
Great work! You’ve learned how to make decisions in Python using:
Remember, just like in apartment hunting, these tools help us make better decisions in any situation where we need to decide whether to take what we have or keep looking for something better!
You will likely find solutions to most exercises online. However, we strongly encourage you to work on these exercises independently without searching explicitly for the exact answers to the exercises. Understanding someone else’s solution is very different from developing your own. Use the lecture notes and try to solve the exercises on your own. This approach will significantly enhance your learning and problem-solving skills.
Remember, the goal is not just to complete the exercises, but to understand the concepts and improve your programming abilities. If you encounter difficulties, review the lecture materials, experiment with different approaches, and don’t hesitate to ask for clarification during class discussions.
Later, you will find the solutions to these exercises online in the associated GitHub repository, but we will also quickly go over them next week. To access the solutions, click on the Github button on the lower right and search for the folder with today’s lecture and tutorial. Alternatively, you can ask ChatGPT or Claude to explain them to you. But please remember, the goal is not just to complete the exercises, but to understand the concepts and improve your programming abilities.
That’s it for part I! Next week, we’ll apply these skills exploration and exploitation!