r/PythonLearning • • 15d ago

Help Request Can someone help me identify what I'm doing wrong?

Post image

it DOES print False!!! WHAT DOES THIS WEBSITE WANT OF ME????

21 Upvotes

33 comments sorted by

15

u/D3str0yTh1ngs 15d ago

It likely doesn't mean the string "False" it means the boolean value False

1

u/Mercy--Main 15d ago

I did try that, also gives the same error!

2

u/Potential_Fix_5007 15d ago

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.

4

u/Im_a_dum_bum 15d ago

they might be strictly looking for if not distance_mi:

3

u/Mercy--Main 15d ago

I've also tried print(False) with both to no avail...

also, that's a way better way of checking that... i complicated myself lmao

2

u/Im_a_dum_bum 15d ago

what website is this?

2

u/Mercy--Main 15d ago

https://www.freecodecamp.org - Seemed like a reputable website when I was looking for sites to learn.

2

u/NerdEnPose 15d ago

Try printing the Flase object not the string. `print(False)`

I’m just guessing.

4

u/nullptr_0x 15d 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. 

1

u/Mercy--Main 15d ago

Yes, it does. All falsy values would equal False when converted into bool, which would then == False and trigger the print code.

1

u/ApprehensiveBrain863 14d ago

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

0

u/nullptr_0x 15d ago

Yeah I think you're right without me trying on my own.

Perhaps a bad test on the site.

Maybe try https://understandit.dev

I made it myself and you can reach out or file issues if you even find a problem.

It has a free programming 101 course in Python.

2

u/0x645 15d ago

what is bool(0)? damm, trudne się wylosowało.

4

u/kedarreddit 15d ago edited 15d 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

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).

1

u/Im_a_dum_bum 15d 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

1

u/Mercy--Main 15d ago

I don't know what to tell you. This is my full code if it helps :/

distance_mi = 0
is_raining = False
has_bike = False
has_car = False
has_ride_share_app = False


if bool(distance_mi) == False:
    print(False)
if distance_mi<= 1 and is_raining == True:
    print("True")
else:
    print("False")
if distance_mi > 1 and distance_mi <= 6:
    if has_bike and is_raining == True:
        print("True")
else:
    print("False")
if distance_mi > 6:
    if has_car:
        print("True")
    elif has_ride_share_app:
        print("True")
else:
    print("False")distance_mi = 0
is_raining = False
has_bike = False
has_car = False
has_ride_share_app = False


if bool(distance_mi) == False:
    print(False)
if distance_mi<= 1 and is_raining == True:
    print("True")
else:
    print("False")
if distance_mi > 1 and distance_mi <= 6:
    if has_bike and is_raining == True:
        print("True")
else:
    print("False")
if distance_mi > 6:
    if has_car:
        print("True")
    elif has_ride_share_app:
        print("True")
else:
    print("False")

4

u/Scolmann 15d ago

Figured it out, it's because you are printing "False\nFalse" but it wants you to print "False" exactly once.

You should rewrite your code in a way that it will only print True or False once

3

u/Mercy--Main 15d ago

oh my god

thank you

2

u/Im_a_dum_bum 15d ago

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

https://stackoverflow.com/help/minimal-reproducible-example

from the site above,

Your code examples should be…

  • Minimal: Use as little code as possible that still produces the same problem

  • Complete: Provide all parts someone else needs to reproduce your problem in the question itself

  • Reproducible: Test the code you're about to provide to make sure it reproduces the problem

2

u/Mercy--Main 15d ago

Will do, thanks.

1

u/Lucky_Cable_3145 15d ago

It looks like it wanted one print output of "False"

Because you used IF to start each block, not ELSE IF, your program prints "False" multiple times.

Something more like

if bool(distance_mi) == False:
    print(False)
else if

2

u/Scolmann 15d ago

Is that a message from rejecting the input, or is that just a hint you can view at any time?

1

u/Mercy--Main 15d ago

when you "submit" the answer and it's incorrect, it tells you why. This is supposedly why I'm failing.

So yes, it's from rejecting the input. The code itself works, the console DOES print False!!!

1

u/BranchLatter4294 15d ago

It looks like it's only expecting one output. You have multiple outputs.

There is also a syntax error in line 25.

1

u/MeasureDoEventThing 15d ago

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`.

2

u/Some-Passenger4219 15d ago

Maybe it meant the bool and not the string literal?

1

u/PTDegen 15d 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 14d 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.

if distance_mi == 0:
    print("False")

Besides that, if you are checking a boolean state, then you should be using is

if bool(distance_mi) is False:
    print("False")

1

u/DannyJons 14d 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 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.

1

u/arivictor 15d ago edited 15d 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 15d 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 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.