Reputation: 31
I'm new to Python and trying to create a weight converter by using if
, else
statements.
This should ask the user to input weight and unit in pounds (lbs) or kilograms (kgs) and convert it, but the program is only executing the if
condition.
Here is the snippet of my code:
weight = int(input("Weight = "))
unit = input('(L)bs or (K)')
if unit == "L" or "l":
print(f'You are {weight*0.45} kilograms')
elif unit == "K" or "k":
print(f'You are {weight*2.2} pounds')
else:
print('The converter is only for Kgs and Lbs')
Here is a screenshot:
Upvotes: 1
Views: 824
Reputation: 1901
When you write:
if unit == 'L' or 'l':
it's getting parsed as:
if (unit == 'L') or ('l'):
which is equivalent to:
if (unit == 'L') or (bool('l') is True):
bool('l')
is always true, which is why none of the elif
/else
blocks get executed.
This is what you intended:
if unit == 'L' or unit == 'l':
If you prefer to avoid the re-use of unit ==
, you can re-write it as:
if unit.upper() == 'L':
or even:
if unit in ('L', 'l'):
I hope this helps.
Addition: (2023-12-13)
An even better way of checking case-insensitive equivalence is to use the str.casefold
method on both strings:
if unit.casefold() == 'L'.casefold():
The str.casefold
method returns a version of its string suitable for caseless comparisons. It might seem a little overkill in the example I just gave, but consider a comparison against "Washington". You could write this:
if name.lower() == "washington":
or you could check against a tuple of possibilities, like this:
if name in ('WASHINGTON', 'Washington', 'washington'):
(Note that this last example is not a true case-less comparison, as it won't match "WashingTon".)
By using str.casefold
you can simply write:
if name.casefold() == "Washington".casefold():
So how is this better than simply writing the slightly shorter:
if name.lower() == "washington":
Well, if you use str.lower
, you'd have to hard-code the string in lower-case, which may not be the canonical form of the string. Which means that if you change the string, you'd have to be careful of keeping it lower-case; if you accidentally use one or more capital letters in your hard-coded string, your comparison will always fail.
If you want to do a case-less comparison against several hard-coded strings, you can map the possibilities with map(str.casefold, (...))
and use in
to check for the choice among the possibilities:
if choice.casefold() in map(str.casefold, ('Y', 'yes', 'Affirmative')):
This check will evaluate to True
if the choice
is yes
, Yes
, YES
, Y
, y
, YeS
, yES
, Affirmative
, affirmative
, AFFIRMative
, etc. (It's nice that you do not have to worry about how you capitalize the possibilities, so you can focus on writing them out using the capitalizations that make the most sense to the program you're writing.)
Upvotes: 1
Reputation: 13
I personally like to sort them out like this:
weight = int(input("Weight = "))
unit = input('(L)bs or (K)')
if unit in ["L", "l"]: #new code
print(f'You are {weight*0.45} kilograms')
elif unit in ["K", "k"]: # new code
print(f'You are {weight*2.2} pounds')
else:
print('The converter is only for Kgs and Lbs')
Upvotes: 1
Reputation: 3756
You need to bring change in two lines:
weight = int(input("Weight: "))
unit = input("(L)bs or (K): ")
# if unit == "L" or "l": # OLD_LINE
if unit == "L" or unit == "l": # NEW_LINE
print(f"You are {weight*0.45} kilograms")
# elif unit == "K" or "k": # OLD_LINE
elif unit == "K" or unit == "k": # NEW_LINE
print(f"You are {weight*2.2} pounds")
else:
print("The converter is only for Kgs and Lbs")
Upvotes: 1
Reputation: 8077
You are lacking parenthesis. Please get familiar with Operator Precedence.
Currently your if
-statements are being read by the interpreter as:
if (unit == "L") or "l":
...
elif (unit == "K") or "k":
...
else:
...
and so the first if
-statement will always be true since bool("l") == True
.
The correct way to do this is:
if unit in "Ll":
...
elif unit in "Kk":
...
else:
...
or even better:
unit = input('(L)bs or (K)g').lower()
if unit == "l":
...
elif unit == "k":
...
else:
...
Upvotes: 1
Reputation: 89
You have to write the full condition before and after "or" For the line:
if unit == "L" or "l"
You have to write:
if unit == "L" or unit == "l"
also the line:
elif unit == "K" or "k"
to
elif unit == "K" or unit == "k"
Upvotes: 1