It means "falsy". There a multiply values that are countet as falsy: -False (the boolian) -0 (the number zero) -""(an empty string) -()(an empty tuple) - -{}(an empty set) -None (a None-object)
To check for falsy you can use "if not" so your if statement would be used if the checked value is falsy. If you use "==" you would check for something like that.
if x == 0: print(falsy)
This small if statement would print while x is 0(the number) and if x is False(the bool). Thats because boolians are a sub-type of int and are seen as 1 and 0. There is also "if x is..." but im not 100% sure in wich cases it works in wich way but that is even a more strict way for checking if inremember correctly.
You're converting distance_mi to a bool and checking if it is equivalent to False. Does that make sense? What is the value and type of bool(distance_mi)
Use the type() function and print function to understand.
If you cast a falsy value to bool, it's not a falsy value anymore because it is now a bool prior to comparison.
You're right in that the outcome is the same, of course that is the entire point of a falsy value, but casting falsy values into boolean types just make them false booleans, and no longer falsy.
This is different to, of course, "if not <value>", which does not involve you explicitly casting the value to a certain type yourself prior to comparison
distance_mi = 0
is_raining = False
has_bike = False
has_car = False
has_ride_share_app = False
if not bool(distance_mi):
print(False)
elif distance_mi <= 1 and not is_raining:
print(True)
elif distance_mi <= 1 and is_raining:
print(False)
elif distance_mi > 1 and distance_mi <= 6:
if not has_bike and is_raining:
print(False)
elif not has_bike and not is_raining:
print(False)
elif has_bike and not is_raining:
print(True)
elif has_bike and is_raining:
print(False)
elif distance_mi > 6:
if has_ride_share_app or has_car:
print(True)
else:
print(False)
Your code was failing because it was printing multiple values instead of a single value. It was also printing the wrong value in some parts of the code. It was also printing the wrong data type (printing a string value instead of a boolean value).
in the future when asking questions, try to include all necessary parts to reproduce the issue, bonus points if it's the smallest amount necessary that causes the issue
You have `print(False)` at first, but then later `print("False")`.
If whatever is checking your code is mocking the print function, it could be seeing those as different, and even it isn't, mixing output types like this is a slightly bad habit.
Also, `not` automatically checks the bool method. So instead of `if bool(distance_mi) == False`, you can just do `if not distance_mi`.
It's been a while since I started python so I forgot how beginners are expected to do this.
With booleans (bool) 0 is considered False but bool(0) is also False. In short you aren't wrong at all but because you did it a funky way, the website might be messing up internally.
Bonus, if you want some fun, the entire statement can be reduced down to print(f"{bool(not distance_mi)}")
It's a typehint telling you you're using an anti-pattern. It's considered bad practice to rely on falsy when you know what the type should be: you've already defined it as a number, you should be checking it as a number instead of casting it to a boolean.
if distance_mi == 0:
print("False")
Besides that, if you are checking a boolean state, then you should be using is
Honestly the website just looks broken. I can make elements of the checklist flip between correct and wrong by altering the initial values assigned to the variables. It seems to evaluate the checklist items using the values you have assigned, so it seems to be impossible to get ticks for everything because all the True conditions get a tick together, and all the False conditions get a tick together.
If I have the program just be print(True) all the True conditions down the left get a tick, and if I make it just print(False) then all the False conditions get a tick. If you print more than one thing all the conditions get a cross.
Curiously
print("Fals", end='') ; print("e", end='')
gets treated as two objects in the pane on the right, but still counts as printing False for the purposes of the checklist.
I am biased with a background in statically-typed languages, like Rust and Java, but I think how you did it is the correct way for it to be done, generally. And I would say they are expecting you to do if not distance_mi, which is "more Pythonic" – and more error-prone if your API, theoretically, ever switches from using int to something else – but, in another comment, you confirmed that didn't work.
Have you found the solution yet? Since I'm interested too.
15
u/D3str0yTh1ngs 15d ago
It likely doesn't mean the string
"False"it means the boolean valueFalse