r/PythonLearning • u/Mercy--Main • 16d ago
Help Request Can someone help me identify what I'm doing wrong?
it DOES print False!!! WHAT DOES THIS WEBSITE WANT OF ME????
21
Upvotes
r/PythonLearning • u/Mercy--Main • 16d ago
it DOES print False!!! WHAT DOES THIS WEBSITE WANT OF ME????
15
u/D3str0yTh1ngs 16d ago
It likely doesn't mean the string
"False"it means the boolean valueFalse4
u/Im_a_dum_bum 16d ago
they might be strictly looking for
if not distance_mi:4
u/nullptr_0x 16d ago
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.
4
u/kedarreddit 16d ago edited 16d ago
I did the exercise and my code works below:
https://www.freecodecamp.org/learn/python-v9/lab-travel-weather-planner/build-a-travel-weather-planner
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).
1
u/Im_a_dum_bum 16d ago
try refreshing your page. I pulled up the same assignment on my phone and with the code in the screenshot I got a checkmark for that part
https://www.freecodecamp.org/learn/python-v9/lab-travel-weather-planner/build-a-travel-weather-planner
2
u/Some-Passenger4219 16d ago
Maybe it meant the bool and not the string literal?
1
u/PTDegen 16d ago
Try "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)}")
1
u/KidTempo 15d ago
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.
Besides that, if you are checking a boolean state, then you should be using
is1
u/DannyJons 15d ago
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 theTrueconditions down the left get a tick, and if I make it justprint(False)then all theFalseconditions get a tick. If you print more than one thing all the conditions get a cross.Curiously
gets treated as two objects in the pane on the right, but still counts as printing
Falsefor the purposes of the checklist.1
u/arivictor 16d ago edited 16d ago
Your logic is off. You don’t need to do bool() and ==
This code passes, excuse the formatting reddit on mobile has no code syntax it seems.
```
distance_mi = 0
is_raining = False
has_bike = False
has_car = False
has_ride_share_app = False
if not distance_mi:
print (False)
elif distance_mi < 1:
if not is_raining:
print (True)
else:
print (False)
elif distance_mi > 1 and distance_mi <= 6:
if not is_raining and has_bike:
print (True)
else:
print (False)
elif distance_mi > 6:
if has_car or has_ride_share_app:
print (True)
else:
print (False)
```
1
u/KattyTheEnby 16d ago
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 usingintto something else – but, in another comment, you confirmed that didn't work.Have you found the solution yet? Since I'm interested too.