-2

I am getting weird outputs like 100kg =103 kg while the lbs to kg conversion is working perfectly fine,

def calculate_1rm(weight_unit, squat, deadlift, bench_press, reps):
    try:
        squat, deadlift, bench_press, reps = float(squat), float(deadlift), float(bench_press), int(reps)
    except ValueError:
        return "Invalid input, please enter a number for all lifts"
    
    if weight_unit.lower() == "kg":
        squat = kg_to_lbs(squat)
        deadlift = kg_to_lbs(deadlift)
        bench_press = kg_to_lbs(bench_press)
        weight_unit = "lbs"
    if weight_unit.lower() == "lbs":
        squat = lbs_to_kg(squat)
        deadlift = lbs_to_kg(deadlift)
        bench_press = lbs_to_kg(bench_press)
        weight_unit = "kg" 

I did not touch the values at all after these conversions

I have tried dividing by /.453592 (same result) and have tried just multiplying the variables (squat,deadlift,bench_press) by 2.205 but no luck

Jason Yang
  • 11,284
  • 2
  • 9
  • 23
Jesus
  • 5
  • 2

1 Answers1

0

Replace your second ‘if’ statement with ‘elif’. Modified code:

def calculate_1rm(weight_unit, squat, deadlift, bench_press, reps):
    try:
        squat, deadlift, bench_press, reps = float(squat), float(deadlift), float(bench_press), int(reps)
    except ValueError:
        return "Invalid input, please enter a number for all lifts"
    
    if weight_unit.lower() == "kg":
        squat = kg_to_lbs(squat)
        deadlift = kg_to_lbs(deadlift)
        bench_press = kg_to_lbs(bench_press)
        weight_unit = "lbs"
    elif weight_unit.lower() == "lbs":
        squat = lbs_to_kg(squat)
        deadlift = lbs_to_kg(deadlift)
        bench_press = lbs_to_kg(bench_press)
        weight_unit = "kg”
Dinux
  • 644
  • 1
  • 6
  • 19