python 基本乘法不起作用,变量kg乘以2.205得到lbs转换

g6ll5ycj  于 2023-04-04  发布在  Python
关注(0)|答案(1)|浏览(76)

我得到奇怪的输出,如100公斤=103公斤,而磅到公斤的转换工作得非常好,

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"

在这些转换之后,我根本没有触及这些值
我试过除以/.453592(相同的结果),并尝试将变量(深蹲,硬拉,卧推)乘以2.205,但没有运气

pxq42qpu

pxq42qpu1#

将第二个'if'语句替换为'elif'。修改的代码:

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”

相关问题