weight_match.py

Weight Formula
 avatar
unknown
plain_text
20 days ago
7.3 kB
2
Indexable
def grams_to_ounces(grams):
    return grams * 0.035274

def ounces_to_grams(ounces):
    return ounces * 28.3495

def pounds_to_kilograms(pounds):
    return pounds * 0.453592

def kilograms_to_pounds(kilograms):
    return kilograms * 2.20462

def weight():
    print("\nWeight Conversions")
    print("="*40)
    print("[1] Grams to Ounces")
    print("[2] Ounces to Grams")
    print("[3] Pounds to Kilograms")
    print("[4] Kilograms to Pounds")
    print("[5] Return to Main Menu")
    print("="*40)

def weight_g_oz():
    while True:
        try:
            print("\nEnter an option (1-5):")
            weight_select = input("-- ")

            if not weight_select.isdigit():
                print("="*50)
                print("INVALID INPUT! Please enter a numeric value.")
                print("="*50)
                return  

            weight_select = int(weight_select)

            match weight_select:
                case 1:  # Grams to Ounces
                    while True:
                        g_to_oz = input("\n[Grams to Ounces Conversion]\nEnter Gram/s: ")

                        if not g_to_oz.replace(".", "", 1).isdigit():
                            print("="*50)
                            print("INVALID INPUT! Please enter a numeric value.")
                            print("="*50)
                            continue  

                        g_to_oz = float(g_to_oz)
                        print(f"{g_to_oz} Gram/s is {grams_to_ounces(g_to_oz):.3f} Ounce/s!")

                        while True:
                            back = input("\n[1] Choose another option \n[2] Stay \n[3] Return to Main Menu\n-- ")

                            if back.isdigit():
                                back = int(back)
                                if back == 1:
                                    return  
                                elif back == 2:
                                    break  
                                elif back == 3:
                                    print("\nReturning to Main menu...")
                                    return "RETURN_MAIN"  
                            else:
                                print("="*50)
                                print("Invalid entry, Please choose 1, 2, or 3.")
                                print("="*50)

                case 2:  # Ounces to Grams
                    while True:
                        oz_to_g = input("\n[Ounces to Grams Conversion]\nEnter Ounce/s: ")

                        if not oz_to_g.replace(".", "", 1).isdigit():
                            print("="*50)
                            print("INVALID INPUT! Please enter a numeric value.")
                            print("="*50)
                            continue    

                        oz_to_g = float(oz_to_g)
                        print(f"{oz_to_g} Ounce/s is {ounces_to_grams(oz_to_g):.3f} Gram/s!")

                        while True:
                            back = input("\n[1] Choose another option \n[2] Stay \n[3] Return to Main Menu\n-- ")

                            if back.isdigit():
                                back = int(back)
                                if back == 1:
                                    return  
                                elif back == 2:
                                    break  
                                elif back == 3:
                                    print("\nReturning to Main menu...")
                                    return "RETURN_MAIN"  
                            else:
                                print("="*50)
                                print("Invalid entry, Please choose 1, 2, or 3.")
                                print("="*50)

                case 3:  # Pounds to Kilograms
                    while True:
                        lb_to_kg = input("\n[Pounds to Kilograms Conversion]\nEnter Pound/s: ")

                        if not lb_to_kg.replace(".", "", 1).isdigit():
                            print("="*50)
                            print("INVALID INPUT! Please enter a numeric value.")
                            print("="*50)
                            continue    

                        lb_to_kg = float(lb_to_kg)
                        print(f"{lb_to_kg} Pound/s is {pounds_to_kilograms(lb_to_kg):.3f} Kilogram/s!")

                        while True:
                            back = input("\n[1] Choose another option \n[2] Stay \n[3] Return to Main Menu\n-- ")

                            if back.isdigit():
                                back = int(back)
                                if back == 1:
                                    return  
                                elif back == 2:
                                    break  
                                elif back == 3:
                                    print("\nReturning to Main menu...")
                                    return "RETURN_MAIN"  
                            else:
                                print("="*50)
                                print("Invalid entry, Please choose 1, 2, or 3.")
                                print("="*50)

                case 4:  # Kilograms to Pounds
                    while True:
                        kg_to_lb = input("\n[Kilograms to Pounds Conversion]\nEnter Kilogram/s: ")

                        if not kg_to_lb.replace(".", "", 1).isdigit():
                            print("="*50)
                            print("INVALID INPUT! Please enter a numeric value.")
                            print("="*50)
                            continue    

                        kg_to_lb = float(kg_to_lb)
                        print(f"{kg_to_lb} Kilogram/s is {kilograms_to_pounds(kg_to_lb):.3f} Pound/s!")

                        while True:
                            back = input("\n[1] Choose another option \n[2] Stay \n[3] Return to Main Menu\n-- ")

                            if back.isdigit():
                                back = int(back)
                                if back == 1:
                                    return  
                                elif back == 2:
                                    break  
                                elif back == 3:
                                    print("\nReturning to Main menu...")
                                    return "RETURN_MAIN" 
                            else:
                                print("="*50)
                                print("Invalid entry, Please choose 1, 2, or 3.")
                                print("="*50)

                case 5:  # Return to Main Menu
                    print("\nReturning to main menu...")
                    return "RETURN_MAIN"

                case _:  # Default case for invalid selection
                    print("="*50)
                    print("Enter a valid selection (1-5)")
                    print("="*50)
                    return 

        except ValueError:
            print("="*50)
            print("INVALID INPUT! Enter a numeric value only.")
            print("="*50)
Editor is loading...
Leave a Comment