I am failing to pass test 18 and 19 even though the terminal returns False as expected when the conditions are seemingly met.
Furthermore, test 11, 12 and 13 requires the use of at least 1 if statement, 1 elif statement and 1 boolean operator (and, or, or not). Does this mean there is a possible solution that only uses 1 instance of each ?
Any general advice or critic are welcome.
Thanks !
18. When the distance is between 1 mile (excluded) and 6 miles (included), and it is raining with no bike, the program should print False.
19. 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.
Your code so far
distance_mi = 0
is_raining = False
has_bike = False
has_car = False
has_ride_share_app = False
# If distance is a falsy value, print Falsy
if distance_mi == 0:
print('Falsy')
# If distance is <= 1 and it isn't raining, print True 1
elif distance_mi <= 1 and is_raining == False:
print('True 1')
# If distance is higher than 1 or <= 6 and the user has a bike and it isn't raining, print True 2
elif distance_mi <= 6 and has_bike == True and is_raining == False:
print('True 2')
# If distance is higher than 6 and the user has a car or a ride share app, pring True 3
elif distance_mi > 6 and has_car == True or has_ride_share_app == True:
print('True 3')
# If none of the above conditions are met, print False
else:
print('False')
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:152.0) Gecko/20100101 Firefox/152.0
Challenge Information:
Build a Travel Weather Planner - Build a Travel Weather Planner
Please organize your code to print only one thing – either True or False.
Remember, that each condition between and/or operators will be evaluated separately, so what can you do to make sure the conditions on each side of the or operator are evaluated together?
Thank you for your reply ! While your suggestion worked it still took me a few hours to understand why.
TL;DR: Because of the incorrect priority of operation in my 3rd elif clause, the latter could evaluate to True even though the >6 miles distance condition was not met.
I will attempt to explain it in case anyone is wondering. Please let me know If I’m missing something or explaining it incorrectly.
From step 17 of Build a Movie Ticket Booking Calculator workshop
“When multiple logical operators are used in an if statement, conditions joined with and are evaluated before conditions joined with or. Parentheses () are used in Python to group conditions and control the order in which they are evaluated.”
So when I take my original clause :
elif distance_mi > 6 and has_car == True or has_ride_share_app == True:
Because the and operator has priority it’s as if the code was operating as such :
elif (distance_mi > 6 and has_car == True) or has_ride_share_app == True:
This created a situation where my 3rd elif clause could evaluate to True even when the distance wasn’t more than 6 miles. In other words, even when the conditions of my 2nd elif clause were False, aside from the distance, my program could still print ‘True’. Hence why my code was failing test 18 and 19 while still passing the others.
Good job! And referencing that bit from the Movie Ticket Booking Calculator challenge should be very helpful to anyone reading this thread in the future…and I intend to use it to help others, too. Thank you for posting that!