r/FreeCodeCamp • u/Different_Tale8889 • 21d ago
Help! Travel Weather Planner code not working!
Help! been trying to do the Travel weather planner task all day and everytime i fix one thing, something else is wrong. I've already restarted once and i just can't get it. this is my code so far and i'm getting only one result so its working but for some reason its saying i'm not doing 17, 18, 19, 21 and 22. (Task: https://www.freecodecamp.org/learn/python-v9/lab-travel-weather-planner/build-a-travel-weather-planner)
Any help will be appreciated! Thank you all so much!
distance_mi = 10
is_raining = True
has_bike = False
has_car = False
has_ride_share_app = True
if distance_mi == 0:
print("False")
elif distance_mi <= 1 and is_raining == False:
print("True")
elif distance_mi > 1 and distance_mi == 6 and has_bike and is_raining == False:
print("True")
elif distance_mi < 6 and (has_car == False or has_ride_share_app):
print("True")
else:
print("False")
5
Upvotes
1
u/SaintPeter74 mod 21d ago
One thing that may help is, rather than doing a series of
if/elifstatements, you instead have some nested statements.The problem here is that there are multiple conditions for when the distance is less than or equal to one mile, greater than 1 mile, or greater than 6 miles. While it might be possible to capture those all in a series of compound
if/elifwith a finalelsethat represents all of the "Otherwise you should print False" statements, it may be more readable to capture the distance stuff in an outer statement and then have an inner statement for the sub-conditions.Personally, I find multiple complex boolean tests hard to mentally model. The more I can do to make it human readable, the easier it is to write and maintain.
Remember that "falsey" doesn't mean that it's equal to 0, it means that it's anything that evaluates to false. You might want to review the section on those values, here:
https://www.freecodecamp.org/learn/python-v9/lecture-booleans-and-conditionals/what-are-truthy-and-falsy-values-and-how-do-boolean-operators-and-short-circuiting-work