python if块中的Print语句从不执行?[duplicate]

rhfm7lfc  于 2022-12-25  发布在  Python
关注(0)|答案(1)|浏览(137)
    • 此问题在此处已有答案**:

How can I read inputs as numbers?(10个答案)
昨天关门了。
下面是代码:

print("Welcome to the weight maintenance calculator!")
startWeight = float(input("Input your start weight, in kilograms: "))
height = float(input("Enter your height in centimetres: "))
sex = input("Enter your sex (M/F): ")
age = float(input("Enter your age: "))
dailyActivity = input("Rank your activity level, from 0-5: ")

if ((sex == "M") or (sex == "m")):
    startWeightBmr = int((10 * startWeight) + (6.25 * height) - (5 * age) + 5)
    if (dailyActivity == 0):
        caloriesUsedCurrently = startWeightBmr + 300
        print(caloriesUsedCurrently)
    if (dailyActivity == 1):
        caloriesUsedCurrently = startWeightBmr + 350
        print(caloriesUsedCurrently)
    if (dailyActivity == 2):
        caloriesUsedCurrently = startWeightBmr + 400
        print(caloriesUsedCurrently)

无论如何,如果运行它,您将非常清楚地看到问题-无论您输入的数字是多少dailyActivity变量caloesUsedCurrently的值为0、1或2。我想知道if块是否有问题,以及它们是否正在执行。有人能指导我吗?它会运行,所以没有错误消息。这让一切都变得更加混乱,请帮帮我!
我试着在IDLE中运行它,看看是否是VSCode的问题,但是没有,因为我怀疑是我的代码。

9nvpjoqh

9nvpjoqh1#

尝试:dailyActivity = int(input("Rank your activity level, from 0-5: "))
代码:

print("Welcome to the weight maintenance calculator!")
startWeight = float(input("Input your start weight, in kilograms: "))
height = float(input("Enter your height in centimetres: "))
sex = input("Enter your sex (M/F): ")
age = float(input("Enter your age: "))
dailyActivity = int(input("Rank your activity level, from 0-5: "))

if ((sex == "M") or (sex == "m")):
    startWeightBmr = int((10 * startWeight) + (6.25 * height) - (5 * age) + 5)
    if (dailyActivity == 0):
        caloriesUsedCurrently = startWeightBmr + 300
        print(caloriesUsedCurrently)
    if (dailyActivity == 1):
        caloriesUsedCurrently = startWeightBmr + 350
        print(caloriesUsedCurrently)
    if (dailyActivity == 2):
        caloriesUsedCurrently = startWeightBmr + 400
        print(caloriesUsedCurrently)
    • 输出:-**
Welcome to the weight maintenance calculator!
Input your start weight, in kilograms: 60
Enter your height in centimetres: 162
Enter your sex (M/F): m
Enter your age: 19
Rank your activity level, from 0-5: 2
1922

相关问题