Home > OS >  trying to make a weight converter but after a input it causes a loop
trying to make a weight converter but after a input it causes a loop

Time:07-24

I'm a noob programmer that has no idea what he's doing so this might be a very simple answer. I am trying to create a simple weight convertor. So you input a random number or your real weight. After this command you get a option to put K or L (Kilos or Pounds). Heres the code:

while True:                              
    try:                                 
        # k or l                         
        weight = int(input("Weight: "))  
        continue                         
    except ValueError:                   
        print("thats not something i know")     
        #tries again due to the person's 
convert_x = weight * 2.20462             
convert_y = weight / 2.20462             
                                         
while True:                              
    try:                                 
        unit = input("(K)g or (L)bs: ")  
    except: unit.upper not in ("K", "L") 

When I try to use this code, I am able to enter a number in but is unable to get to the next step of choosing Kilos Or Pounds. The code ends up repeating and ending up having to input a number again. If you put anything else other than a number you get greeted with a "i dont understand that" and you're in a loop. i also want to know how to do the same with the K and L without being redundant. While also keeping the numbers to be at the hundredths.

CodePudding user response:

The issue is that you've never provided a way for the code to break out of the loop. By using "while True", you're saying to loop until it's not true; since you haven't set a condition to be met, that's never going to happen. If you don't want to change the loop logic itself, you can break out of it by using "break instead of "continue" like so.

while True:                              
    try:                                 
        # k or l                         
        weight = int(input("Weight: "))  
        break                        
    except ValueError:                   
        print("thats not something i know")     
        #tries again due to the person's 
convert_x = weight * 2.20462             
convert_y = weight / 2.20462             
                                         
while True:                              
    try:                                 
        unit = input("(K)g or (L)bs: ")
        break  
    except: unit.upper not in ("K", "L") 

"Continue" breaks only out of that particular iteration of the loop, not the loop in its entirety, so program control at that point goes back to the "while True:" line and you're right back where you started. By using "break" instead, you're able to exit those particular code blocks if acceptable input is received.

Here's a resource for learning more about this type of control flow: https://www.geeksforgeeks.org/how-to-use-while-true-in-python/

CodePudding user response:

I copied your code and did some manual debugging of the code. Some of the issues as noted were that you were using a "continue" call instead of a "break" call in your "while" loops. Also, where you were wanting to ensure that the entered character was an uppercase value, you had appended ".upper" instead of ".upper()" which is what you want. With that feel free to review the following code with some revisions which I believe follow the spirit of your code.

while True:                              
    try:                                                   
        weight = int(input("Weight: "))  
        break                         
    except ValueError:                   
        print("thats not something i know")     
        #tries again due to the person's 
                                         
while True:                              
    unit = input("(K)g or (L)bs: ")  
    if unit.upper() not in ["K", "L"]:
        print("Please try again") 
    else:
        break
if unit.upper() == "L":
    kilos = int(weight / 2.20462   .5)
    pounds = weight
else:
    kilos = weight
    pounds = int(weight * 2.20462   .5)  

print("Weight in kilograms:", kilos)
print("Weight in pounds:", pounds)

I added some rounding to the calculations to assist. Give that a look. I hope that clarifies things.

Regards.

  • Related