curriculum/challenges/english/blocks/lab-travel-weather-planner/694acade1d4afdbce71e5840.md
For this lab, you will use conditional statements to determine whether commuting is possible based on the weather, the distance to travel, and the availability of a vehicle.
Objective: Fulfill the user stories below and get all the tests to pass to complete the lab.
distance_mi (a number representing the distance to travel in miles)is_raining (a boolean representing if the user is currently experiencing rainy weather)has_bike (a boolean representing if the user has a bicycle)has_car (a boolean representing if the user has a car)has_ride_share_app (a boolean representing if the user has an app that allows them to request a ride)if, elif, and else statements to evaluate the distance categories in ascending order.distance_mi is a falsy value:
False.True only if it is not raining.False.True only if the person has a bike and it is not raining.False.True if the person has a car or has a ride-share app.False.You should have a variable named distance_mi.
({ test: () => runPython(`assert _Node(_code).has_variable("distance_mi")`) })
You should assign a number to your distance_mi variable.
({ test: () => runPython(`assert isinstance(distance_mi, (int, float))`) })
You should have a variable named is_raining.
({ test: () => runPython(`assert _Node(_code).has_variable("is_raining")`) })
You should assign a boolean to your is_raining variable.
({ test: () => runPython(`assert isinstance(is_raining, bool)`) })
You should have a variable named has_bike.
({ test: () => runPython(`assert _Node(_code).has_variable("has_bike")`) })
You should assign a boolean to your has_bike variable.
({ test: () => runPython(`assert isinstance(has_bike, bool)`) })
You should have a variable named has_car.
({ test: () => runPython(`assert _Node(_code).has_variable("has_car")`) })
You should assign a boolean to your has_car variable.
({ test: () => runPython(`assert isinstance(has_car, bool)`) })
You should have a variable named has_ride_share_app.
({ test: () => runPython(`assert _Node(_code).has_variable("has_ride_share_app")`) })
You should assign a boolean to your has_ride_share_app variable.
({ test: () => runPython(`assert isinstance(has_ride_share_app, bool)`) })
You should use at least one if statement.
({ test: () => runPython(`
import ast
tree = ast.parse(_code)
ifs = [node for node in ast.walk(tree) if isinstance(node, ast.If)]
assert len(ifs) >= 1
`) })
You should use at least one elif branch in your program.
({ test: () => runPython(`
import ast
tree = ast.parse(_code)
elifs = []
for node in ast.walk(tree):
if isinstance(node, ast.If) and node.orelse:
if isinstance(node.orelse[0], ast.If):
elifs.append(node)
assert len(elifs) >= 1
`) })
You should use at least one boolean operator (and, or, or not) in your code.
({ test: () => runPython(`
import ast
tree = ast.parse(_code)
bool_ops = [
node for node in ast.walk(tree)
if isinstance(node, (ast.BoolOp, ast.UnaryOp))
]
assert len(bool_ops) >= 1
`) })
You should use the print() function to display the result.
({ test: () => runPython(`assert _Node(_code).block_has_call("print")`) })
When distance_mi is a falsy value, the program should print False.
({ test: () => runPython(`
import ast, io, contextlib
VARIABLES = {
"distance_mi",
"is_raining",
"has_bike",
"has_car",
"has_ride_share_app"
}
def run_case(env, expected):
tree = ast.parse(_code)
tree.body = [
node for node in tree.body
if not (
isinstance(node, ast.Assign)
and isinstance(node.targets[0], ast.Name)
and node.targets[0].id in VARIABLES
)
]
clean_code = compile(tree, "<ast>", "exec")
buffer = io.StringIO()
with contextlib.redirect_stdout(buffer):
exec(clean_code, env)
assert buffer.getvalue().strip() == expected
run_case(
{
"distance_mi": 0,
"is_raining": False,
"has_bike": True,
"has_car": True,
"has_ride_share_app": True
},
"False"
)
run_case(
{
"distance_mi": 0.0,
"is_raining": False,
"has_bike": True,
"has_car": True,
"has_ride_share_app": True
},
"False"
)
`) })
When the distance is 1 mile or less and it is not raining, the program should print True.
({ test: () => runPython(`
import ast, io, contextlib
VARIABLES = {
"distance_mi",
"is_raining",
"has_bike",
"has_car",
"has_ride_share_app"
}
def run_case(env, expected):
tree = ast.parse(_code)
tree.body = [
node for node in tree.body
if not (
isinstance(node, ast.Assign)
and isinstance(node.targets[0], ast.Name)
and node.targets[0].id in VARIABLES
)
]
clean_code = compile(tree, "<ast>", "exec")
buffer = io.StringIO()
with contextlib.redirect_stdout(buffer):
exec(clean_code, env)
assert buffer.getvalue().strip() == expected
run_case(
{
"distance_mi": 0.5,
"is_raining": False,
"has_bike": False,
"has_car": False,
"has_ride_share_app": False
},
"True"
)
run_case(
{
"distance_mi": 1,
"is_raining": False,
"has_bike": False,
"has_car": False,
"has_ride_share_app": False
},
"True"
)
`) })
When the distance is 1 mile or less and it is raining, the program should print False.
({ test: () => runPython(`
import ast, io, contextlib
VARIABLES = {
"distance_mi",
"is_raining",
"has_bike",
"has_car",
"has_ride_share_app"
}
def run_case(env, expected):
tree = ast.parse(_code)
tree.body = [
node for node in tree.body
if not (
isinstance(node, ast.Assign)
and isinstance(node.targets[0], ast.Name)
and node.targets[0].id in VARIABLES
)
]
clean_code = compile(tree, "<ast>", "exec")
buffer = io.StringIO()
with contextlib.redirect_stdout(buffer):
exec(clean_code, env)
assert buffer.getvalue().strip() == expected
run_case(
{
"distance_mi": 0.5,
"is_raining": True,
"has_bike": False,
"has_car": False,
"has_ride_share_app": False
},
"False"
)
run_case(
{
"distance_mi": 1,
"is_raining": True,
"has_bike": False,
"has_car": False,
"has_ride_share_app": False
},
"False"
)
`) })
When the distance is between 1 mile (excluded) and 6 miles (included), and it is raining with no bike, the program should print False.
({ test: () => runPython(`
import ast, io, contextlib
VARIABLES = {
"distance_mi",
"is_raining",
"has_bike",
"has_car",
"has_ride_share_app"
}
def run_case(env, expected):
tree = ast.parse(_code)
tree.body = [
node for node in tree.body
if not (
isinstance(node, ast.Assign)
and isinstance(node.targets[0], ast.Name)
and node.targets[0].id in VARIABLES
)
]
clean_code = compile(tree, "<ast>", "exec")
buffer = io.StringIO()
with contextlib.redirect_stdout(buffer):
exec(clean_code, env)
assert buffer.getvalue().strip() == expected
run_case(
{
"distance_mi": 2,
"is_raining": True,
"has_bike": False,
"has_car": True,
"has_ride_share_app": True
},
"False"
)
run_case(
{
"distance_mi": 4,
"is_raining": True,
"has_bike": False,
"has_car": True,
"has_ride_share_app": True
},
"False"
)
run_case(
{
"distance_mi": 6,
"is_raining": True,
"has_bike": False,
"has_car": True,
"has_ride_share_app": True
},
"False"
)
`) })
When the distance is between 1 mile (excluded) and 6 miles (included), it is not raining but no bike is available, the program should print False.
({ test: () => runPython(`
import ast, io, contextlib
VARIABLES = {
"distance_mi",
"is_raining",
"has_bike",
"has_car",
"has_ride_share_app"
}
def run_case(env, expected):
tree = ast.parse(_code)
tree.body = [
node for node in tree.body
if not (
isinstance(node, ast.Assign)
and isinstance(node.targets[0], ast.Name)
and node.targets[0].id in VARIABLES
)
]
clean_code = compile(tree, "<ast>", "exec")
buffer = io.StringIO()
with contextlib.redirect_stdout(buffer):
exec(clean_code, env)
assert buffer.getvalue().strip() == expected
run_case(
{
"distance_mi": 2,
"is_raining": False,
"has_bike": False,
"has_car": True,
"has_ride_share_app": True
},
"False"
)
run_case(
{
"distance_mi": 5,
"is_raining": False,
"has_bike": False,
"has_car": True,
"has_ride_share_app": True
},
"False"
)
run_case(
{
"distance_mi": 6,
"is_raining": False,
"has_bike": False,
"has_car": True,
"has_ride_share_app": True
},
"False"
)
`) })
When the distance is between 1 mile (excluded) and 6 miles (included), a bike is available, and it is not raining, the program should print True.
({ test: () => runPython(`
import ast, io, contextlib
VARIABLES = {
"distance_mi",
"is_raining",
"has_bike",
"has_car",
"has_ride_share_app"
}
def run_case(env, expected):
tree = ast.parse(_code)
tree.body = [
node for node in tree.body
if not (
isinstance(node, ast.Assign)
and isinstance(node.targets[0], ast.Name)
and node.targets[0].id in VARIABLES
)
]
clean_code = compile(tree, "<ast>", "exec")
buffer = io.StringIO()
with contextlib.redirect_stdout(buffer):
exec(clean_code, env)
assert buffer.getvalue().strip() == expected
run_case(
{
"distance_mi": 2,
"is_raining": False,
"has_bike": True,
"has_car": False,
"has_ride_share_app": False
},
"True"
)
run_case(
{
"distance_mi": 5,
"is_raining": False,
"has_bike": True,
"has_car": False,
"has_ride_share_app": False
},
"True"
)
run_case(
{
"distance_mi": 6,
"is_raining": False,
"has_bike": True,
"has_car": False,
"has_ride_share_app": False
},
"True"
)
`) })
When the distance is greater than 6 miles and a ride share app is available, the program should print True.
({ test: () => runPython(`
import ast, io, contextlib
VARIABLES = {
"distance_mi",
"is_raining",
"has_bike",
"has_car",
"has_ride_share_app"
}
def run_case(env, expected):
tree = ast.parse(_code)
tree.body = [
node for node in tree.body
if not (
isinstance(node, ast.Assign)
and isinstance(node.targets[0], ast.Name)
and node.targets[0].id in VARIABLES
)
]
clean_code = compile(tree, "<ast>", "exec")
buffer = io.StringIO()
with contextlib.redirect_stdout(buffer):
exec(clean_code, env)
assert buffer.getvalue().strip() == expected
run_case(
{
"distance_mi": 12,
"is_raining": False,
"has_bike": False,
"has_car": False,
"has_ride_share_app": True
},
"True"
)
`) })
When the distance is greater than 6 miles and a car is available, the program should print True.
({ test: () => runPython(`
import ast, io, contextlib
VARIABLES = {
"distance_mi",
"is_raining",
"has_bike",
"has_car",
"has_ride_share_app"
}
def run_case(env, expected):
tree = ast.parse(_code)
tree.body = [
node for node in tree.body
if not (
isinstance(node, ast.Assign)
and isinstance(node.targets[0], ast.Name)
and node.targets[0].id in VARIABLES
)
]
clean_code = compile(tree, "<ast>", "exec")
buffer = io.StringIO()
with contextlib.redirect_stdout(buffer):
exec(clean_code, env)
assert buffer.getvalue().strip() == expected
run_case(
{
"distance_mi": 12,
"is_raining": False,
"has_bike": True,
"has_car": True,
"has_ride_share_app": False
},
"True"
)
`) })
When the distance is greater than 6 miles and no car nor a ride share app is available, the program should print False.
({ test: () => runPython(`
import ast, io, contextlib
VARIABLES = {
"distance_mi",
"is_raining",
"has_bike",
"has_car",
"has_ride_share_app"
}
def run_case(env, expected):
tree = ast.parse(_code)
tree.body = [
node for node in tree.body
if not (
isinstance(node, ast.Assign)
and isinstance(node.targets[0], ast.Name)
and node.targets[0].id in VARIABLES
)
]
clean_code = compile(tree, "<ast>", "exec")
buffer = io.StringIO()
with contextlib.redirect_stdout(buffer):
exec(clean_code, env)
assert buffer.getvalue().strip() == expected
run_case(
{
"distance_mi": 12,
"is_raining": False,
"has_bike": True,
"has_car": False,
"has_ride_share_app": False
},
"False"
)
`) })
distance_mi = 12
is_raining = False
has_bike = False
has_car = False
has_ride_share_app = True
if not distance_mi:
print(False)
elif distance_mi <= 1:
print(not is_raining)
elif distance_mi <= 6:
print(has_bike and not is_raining)
else:
print(has_car or has_ride_share_app)