Most basic error

unit = input("(K)gs or (L)bs: ")
weight = float(input("Weight: "))
if unit == "K" or "k":
    weight = weight * .4536
    print("Weight in Kgs: " + str(weight))
elif unit == "L" or "l":
    weight = weight / .4536
    print("Weight in Lbs: " + str(weight))

so i am brand new to python and i had a version of this code working but i changed it then changed it back and now it wont work for some reason regardless if the unit variable is inputed as L,l,K,k it always runs the if sequence which is really bothering me and i cant seem to figure out why it would run the line without the variable actually equaling the required value gonna post the code will delete once answered thank you

Welcome to the forum.

Please format your code:

It’s especially important for Python I guess, because, you know, these indentations.

in the tutorial im watching there is no such thing needed im using the pycharm editor

I understand.
I was talking about your post here, specifically.

gotcha no worries do you know why it would run like it would run the if sequence regardless of inputed variable?

i dont understand why it would matter if i formatted my code in the way above i do have indentations on my code but they dont translate to this forum

Well, it would make oyur code more readable for all who can see it.
The more readable your code - the bigger possibility that someone will actually be able to help you with your questions.

Have oyu ever tried to read unformatted code written by other folks? It’s not really exciting experience :slightly_smiling_face:

Thank you my brain is being funny after a long day at work got it fixed

it really doesnt make much sense to me why itd be running the if line if the requirements arent met it has to be something so obvious i just cant see it

you mean cases when user will enter some unexpected nonsense?

I entered into the weight this garbage:

dfgberigrfuy

It throwed value error, obviously, cause it can not convert this stuff to float

doesnt matter if put in in l or k variable it always outputs in kgs
the garbage im not worried about right now more stuck on my current issue haha
with the weight variable you would always put in a number or your gonna get that error you said and the unit variable would always be L,l,k,K

strong text


output should have been “Weight in Lbs: 264.5”

Yeah, i see it. I am trying to switch my brain from JS to python and recall this stuff. It is something simple I am sure.

no problemo amigo this has just stumped me even though i literally just started and it got my brain hurting a little

figured it out
if statements are incorrect

rewrite them like this:

if unit == "K" or unit == "k":

unit == ‘k’ is what you need here

‘k’ is the condition which will always be true, so your code always going to jump into first if block

1 Like