1

I just start learning python. I built a basic weight converter which can convert weight kilo to pound or pound to kilo. I have two input field. I want only accept value "L" or "K" in first input filed if user give wrong input then it will show the error massage like "please choose between k or L" and I also want if user give any string value in second input field then it will show a error massage like "please enter number. You given string"

here is my code:

unit = input("Select (K)kilograms or (L)pounds: ").upper()
if unit == "L":
            weight = int(float(input("give your weight: ")))
            converted_weight = weight * 0.45
            print(f"your weight {converted_weight} converted to kilograms")

elif unit == "K":
            weight = int(float(input("give your weight: ")))
            converted_weight = weight / 0.45
            print(f"your weight {converted_weight} converted to pounds")
boyenec
  • 1,405
  • 5
  • 29
  • try python assertions. – goku Sep 10 '20 at 20:04
  • "I just start learning python" Then you should follow a tutorial. Stack Overflow is not the place to teach you a programming language from the beginning. – Karl Knechtel Sep 10 '20 at 20:07
  • Karl Knechtel after saw the tutorial I built the basic calculator myself. If you don't want to help then it's okay. Before posting my topics I did google and aslo searched on stackoverflow. As I didn't find the solution so I post my problem here. – boyenec Sep 10 '20 at 20:17

3 Answers3

1
unit = input("Select (K)kilograms or (L)pounds: ").upper()
if unit == "L":
    weight = int(float(input("give your weight: ")))
    converted_weight = weight * 0.45
    print(f"your weight {converted_weight} converted to kilograms")

elif unit == "K":
    weight = int(float(input("give your weight: ")))
    converted_weight = weight / 0.45
    print(f"your weight {converted_weight} converted to pounds")
else:
    raise ValueError("please choose between k or L")

this will help you

0

You can use while loop and you can show the your desire error massage if you didn't get right input from user.

here is the full code:

while True:
    try:
        unit = input("Select (K)kilograms or (L)pounds: ").upper()
        if unit == "L":
            weight = int(float(input("give your weight: ")))
            converted_weight = weight * 0.45
            print(f"your weight {converted_weight} converted to kilograms")
            break
        elif unit == "K":
            weight = int(float(input("give your weight: ")))
            converted_weight = weight / 0.45
            print(f"your weight {converted_weight} converted to pounds")
            break
        elif unit != "K" or "L": #it will show error massage if user give anything except k or l
            print(""please choose between k or L")
            continue
    except ValueError:
        print("please enter number. You given string") 
        continue
Farhan Ahmed
  • 192
  • 1
  • 13
0

First I'll suggest having those tests in a different function. As for the code itself, you may want somethink like this:

try:
    assert input1 in ['L','K']
except AssertionError as e:
    e.args += ('please choose between k or L')
    raise
try:
    assert type(input2) == int
except AssertionError as e:
    e.args += ('please enter number, Wrong type for input2')
    raise
HadarM
  • 113
  • 1
  • 9