0

What's poppin my coding gang. So atm I'm learning Python and I'm a totally a newbie and I face this problem. So I created a unit converting program and I was successful to make a while loop for unit and everything with the code below works just fine:

weight = int(input("Weight: "))
unit = input("(K)g or (L)bs ? ")

while unit.upper != ("K" or "L"):
    if unit.upper()=="L":
        converted = weight*0.45
        print(f"You are {round(converted)} kilos")
        break
    elif unit.upper()=="K":
        converted=weight//0.45
        print(f"You are {converted} pounds")
        break
    else:
        print("Invalid unit. Please type K or L: ")
        unit=input()
        continue

But I also wanted to experiment more and I also wanted to create a while loop for a weight input, so that it will go forever until you type any positive float or integer number, because when I run the program and in weight input I would accidently type a letter - a big red error would appear on my screen saying:

Exception has occurred: ValueError
invalid literal for int() with base 10: 'a'
  line 1, in <module>
    weight = int(input("Weight: "))

So when I tried to change it to a while loop, it didn't work and my final result looked like this:

weight = int(input("Weight: "))

while weight != int():
    if weight==int():
        break
    else:
        print("Invalid unit. Please type a number: ")
        weight=int(input())
        continue

unit = input("(K)g or (L)bs ? ")

while unit.upper != ("K" or "L"):
    if unit.upper()=="L":
        converted = weight*0.45
        print(f"You are {round(converted)} kilos")
        break
    elif unit.upper()=="K":
        converted=weight//0.45
        print(f"You are {converted} pounds")
        break
    else:
        print("Invalid unit. Please type K or L: ")
        unit=input()
        continue

I know it's shit and at this point I'm stuck, it's just constantly typing me "Invalid unit. Please type a number: " and I can't get out of that loop. I don't even know what to type or what to do anymore so I decided to come here for a help.

I want to make with this code that until you type a number in weight input - you won't be allowed to go further, but after you type correctly - the program will continue to a unit input. Thx

  • This line in your first code doesn't make sense `unit.upper != ("K" or "L")` upper is a method of `str` it will never be equal to `"K"`. I say `"K"` because `"K" or "L"` will always evaluate to that (the first truthy value). Basically your code looks to work just because of the breaks. – Abdul Aziz Barkat Feb 14 '21 at 16:35
  • 1
    Also if you execute `int()` you will find that it just gives you the integer `0` meaning you are basically checking if the number is `0` or not. – Abdul Aziz Barkat Feb 14 '21 at 16:41

2 Answers2

0

The or operator does not work as you expect.

I suggest to replace it by:

while unit.upper() in "KL":
guidot
  • 5,095
  • 2
  • 25
  • 37
0

✅ Tested - I think you need to check the input for weight like this:

weight = input("Weight: ")

while not weight.isdigit():
    print("Invalid unit. Please type a number... ")
    weight= input("Weight: ")

  • Note that using `isdigit` is rather limited. You couldn't use it for floats like `2.3` or `3e6`, for example. The 'traditional' Python way is to convert to int/float in a `try` block. – Thierry Lathuille Feb 14 '21 at 16:47