Python Forum
Help with basic weight converter
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with basic weight converter
#1
Ok so I'm brand new to coding and following a YouTube video for learning python, one of the exercises is to make a simple weight conversion program. I thought I understood it but I'm at a complete loss. The user enters their weight, then it asks if that was in kgs. or lbs. if the user enters a k (higher or lower case) then it will convert to lbs and vice versa. Now the problem I'm having is that the script runs but only outputs the first if statement. I have no idea why. This is my code

weight = float(input("Enter your weight "))
unit = input(" Is that (K)Kgs or (L)lbs ")
conversion = 0.45
if unit == "K" or "k":
    convertlbs = (float)(weight * conversion)
    print (convertlbs)
elif unit == "L" or "l":
    convertkgs = (float)(weight / conversion)
    print (convertkgs)
else:
    print ("wrong input")
Could someone please explain why this is not working? From what I understand the first if statement is checking to see if the user input for unit is equal to K or k, if it is then it should times their weight by 0.45, if its not true then it should move onto the next if statement to see if its an L or l, then dividing my 0.45. If neither of these inputs are true then it should print wrong input

Reply
#2
Non-empty strings, e.g. "k" on line 4 evaluate to True, so your or expression is wrong. The right hand side needs to check unit == "k".
Reply
#3
(Jun-29-2022, 02:16 PM)ndc85430 Wrote: Non-empty strings, e.g. "k" on line 4 evaluate to True, so your or expression is wrong. The right hand side needs to check unit == "k".

I can't believe it was that simple! makes perfect sense now that I can see it. I've been pulling my hair out for about an hour with this
Reply
#4
"or" is a logical operator. You are trying to use it more like a union.

"k" or "K" tests if "k" is true-ey. If so it returns "k". If not it returns "K". Most objects in Python are true-ey. Python objects that are false-ey are False, None, 0, and empty collections. "k" is a non-empty string, so it is true-ey, and "k" or "K" returns "k".

"and" is similar. "k" and "K" returns "K" if both "k" and "K" are true-ey, else it returns False. Both "k" and "K" are non-empty strings, so "k" and "K" returns "K".

You want to test if unit == "k" or unit == "K". You could also force unit to be upper or lower case: unit.upper() == "K" or you could test if unit is in a collection: unit in ("k", "K"). You might also see: unit in "kK", but this would return True if the user entered "k", "K" or "kK".
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Weight Distribution 11drk9 11 695 Mar-13-2024, 06:08 AM
Last Post: Pedroski55
  currency converter using forex-python preethy12ka4 8 730 Mar-08-2024, 06:59 PM
Last Post: preethy12ka4
  Python Networkx: Visualize an edge weight with a bubble/circle uvw 0 2,010 Sep-01-2021, 06:26 AM
Last Post: uvw
  Converter Souls99 2 2,394 Aug-01-2020, 01:27 AM
Last Post: Souls99
  Help me get this image converter code working? NeTgHoSt 0 2,089 Jul-14-2020, 10:36 PM
Last Post: NeTgHoSt
  Currency converter Scott 5 3,007 Jun-14-2020, 11:59 PM
Last Post: Scott
  How to use 2to3.py converter under Windows OS samsonite 2 7,324 Mar-02-2019, 05:49 AM
Last Post: samsonite
  Can't get a downloaded file converter to work Godotisnothere 1 3,188 Jan-24-2017, 06:01 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020