Python中的循环终止程序

时间:2018-04-29 20:02:23

标签: python loops whitespace

我是编码和Python的新手,我无法解决这个问题。 我添加了循环,以便程序不会在每次出现拼写错误时终止,但是我可能会把第二个循环搞砸了。 我认为它可能与空白有关,但我无法弄明白。 程序在接受" calories_burned"之后终止。变量

def start():

print("""This is a Weight Calculator, please follow the instructions.""")
name = input("What is your name? ")
age = int(input("How old are you? "))

if 19 <= age <= 78:

    height = float(input("How tall are you in cm? "))
    current_weight = float(input("How much do you currently weigh? "))
    goal_weight = float(input("How much do you want to weigh? "))
    calories_consumed = int(input("How many calories did you consume today? "))
    calories_burned = int(input("How many calories did you burn today? "))

    def activity():

        global rmr
        activity_factor = input("Choose an activity factor: Sedentary, Lightly active, Moderately Active, Very Active ")

        zif activity_factor == "sedentary":
            rmr = int(round(((10 * current_weight) + (6.25 * height) - (5 * age) + 5) * 1.2))

        if activity_factor == "lightly active":
            rmr = int(round((10 * current_weight + 6.25 * height - 5 * age + 5) * 1.375))

        if activity_factor == "moderately active":
            rmr = int(round(((10 * current_weight) + (6.25 * height) - (5 * age) + 5) * 1.550))

        if activity_factor == "very active":
            rmr = int(round(((10 * current_weight) + (6.25 * height) - (5 * age) + 5) * 1.725))

        else:
            activity()

        daily_deficit = int((rmr + calories_burned - calories_consumed))
        print("Your daily deficit is: " + str(daily_deficit))

        import numpy
        mean_deficit = [daily_deficit]
        calculation_mean_deficit = int(round(7500 / numpy.mean(mean_deficit)))
        reach_goal = int(round((current_weight - goal_weight) * calculation_mean_deficit))

        print(mean_deficit)
        print(name + " it will take you " + str(calculation_mean_deficit) + " days to loose 1 kg.")
        print(name + " it will take you " + str(reach_goal) + " days to reach your goal.")

        choice = input("do you wish to continue Y/N?")

        if choice == "n":
            return
        else:
            start()
else:
    print("Invalid age")
    start()


start()

1 个答案:

答案 0 :(得分:0)

您不会在代码中的任何位置调用activity()函数,因此一旦程序收集输入,它就不会执行任何操作并退出。

此外,它“失去”而不是“松散”