Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8336579
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T04:02:10+00:00 2026-06-09T04:02:10+00:00

I am trying write a program in python to calculate heart rate training zones

  • 0

I am trying write a program in python to calculate heart rate training zones (using Karvonen’s formula for anyone who is interested 🙂 ).

The formula is different for men and women but both require age (alder), resting heart rate (hvilepuls) and max heart rate (makspuls).

The code calculates max heart rate if the individual just presses enter.

I wanted to catch blank inputs for age and resting heart, and also values inputted for all three variables that are zero or less.

I have been able to catch blank inputs but I cannot seem to combine it with the zero or less ones as well.

The code I have written is below and works but does not stop people enter numbers that are equal to or less than zero.

Any other general comments about cleaning-up the code and better (more pythonic perhaps?) ways of writing this are much appreciated.

# Karvonens formel
#
print(
"""
Kalkulere dine treningssoner for lett løping, anaerob terskel (AT) og VO2 Max treningsøkter.

Instruksjoner

1. Fyll inn din alder, hvilepuls og kjønn.
2. Skriv inn din maksimale hjertefrekvens, hvis du vet det, ellers trykk enter - deretter beregnet programmet det selv som følger:
(Menn 214 - (0,8 * alder) Kvinner:. 209 - (0,7 * alder).

3. Treningssonenes verdier beregnes ved hjelp av Karvonen formelen:
X% = (Maksimal hjertefrekvens hvilepuls) * x/100) + hvilepuls
"""
)

kjonn=""
alder=""
hvilepuls=""
makpuls=""


while kjonn.lower() != "m" and kjonn.lower() != "d":
    kjonn = input("Hvilken kjønn er du? (M)ann/(D)ame\t")

while alder=="":
    alder = int(input("Hvor mange år er du?\t"))

while hvilepuls=="":
    hvilepuls = int(input("Hva er din hvilepuls?\t"))

makspuls = input("Hva er din makspuls?\t")


if makspuls=="":
    if kjonn.lower()=="m":
        makspuls = int(214 - 0.8*int(alder))
    elif kjonn.lower() =="d":
        makspuls = int(209 - 0.7*int(alder))
    else:
        print("\n\nEnter M or D")
else:
    makspuls=int(makspuls)

prosent60 = (makspuls-hvilepuls)*0.6 + hvilepuls
prosent75 = (makspuls-hvilepuls)*0.75 + hvilepuls
prosent85 = (makspuls-hvilepuls)*0.85 + hvilepuls
prosent90 = (makspuls-hvilepuls)*0.9 + hvilepuls

# Remove decimals
prosent60 = int(prosent60)
prosent75 = int(prosent75)
prosent85 = int(prosent85)
prosent90 = int(prosent90)


print("\n\n")
if kjonn.lower()=="m":
    print("Din kjønn  er:\t\tMann")
else:
    print("Din kjønn  er:\t\tDame")
print("Din alder er:\t\t", alder)
print("Din hvilepuls er:\t", hvilepuls," slag/min")
print("Din makspuls er:\t", makspuls," slag/min")

print("\n\n")
print("Din lavsone er:\t",prosent60,"-",prosent75," slag/min")
print("Din ATsone er:\t",prosent85,"-",prosent90," slag/min")
print("Din VO2max er:\t",prosent90,"-",makspuls," slag/min")


input("\n\nTrykk enter for å avslutte")
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T04:02:13+00:00Added an answer on June 9, 2026 at 4:02 am

    Here is what I suggest.

    • Input should be validated, and cleaned up, immediately. Usually when you take input, it is a good idea to call the .strip() method, in case the user entered extra spaces or tabs by mistake. Right after you read the 'm' or 'd' value, force it to lower-case; then the rest of the code can assume it is lower-case.

    • When you want to check one value against multiple possible legal values, a good Python idiom is to use in (value0, value1, value2, ..., valueN). In this case you could check in ('m', 'd'), but because we are checking for a single character, we can actually just check in "md" and that works too.

    • When you want to allow an empty string or an integer, just read the response into a temp variable (I used the name s for “string temporary variable”) and then you can check for a zero-length string; if it is not zero-length, convert to an integer. You could initialize your integer variable to a negative value, and then loop until it is not negative; but I prefer to use the while True: loop and add an explicit break to show the success condition that breaks out. (Note: if this is homework, your teacher may not agree with me; some teachers don’t like to see break inside a loop, but rather prefer testing the loop only at the top.)

    • I added some error messages. I put “@@@@@” around my text to help make sure you find it all and localize it to your language. (It’s a short program so likely you could have found them all anyway, but I wanted to make it as easy as possible for you.)

    • Instead of calling int() on floating-point numbers to convert them to integer, I used round(), so that 77.9 will become 78 rather than 77.

    EDIT: I just edited the code. I broke out the input validation into a function, with a “validator” function you pass in. I think it is cleaner this way.

    Code:

    # Karvonens formel
    #
    print(
    """
    Kalkulere dine treningssoner for lett lping, anaerob terskel (AT) og VO2 Max treningskter.
    
    Instruksjoner
    
    1. Fyll inn din alder, hvilepuls og kjnn.
    2. Skriv inn din maksimale hjertefrekvens, hvis du vet det, ellers trykk enter - deretter beregnet programmet det selv som flger:
    (Menn 214 - (0,8 * alder) Kvinner:. 209 - (0,7 * alder).
    
    3. Treningssonenes verdier beregnes ved hjelp av Karvonen formelen:
    X% = (Maksimal hjertefrekvens hvilepuls) * x/100) + hvilepuls
    """
    )
    
    kjonn=' '
    alder=0
    hvilepuls=0
    makpuls=' '
    
    def check_positive(n):
        if n > 0:
            return True
        else:
            print("@@@@@ Cannot enter a negative or 0 value! @@@@@")
            return False
    def check_positive_or_zero(n):
        if n >= 0:
            return True
        else:
            print("@@@@@ Cannot enter a negative value! @@@@@")
            return False
    
    def get_input_int(s_mesg, fn_validate, default=None):
        while True:
            s = input(s_mesg).strip()
            if not s and default is not None:
                return default
            # not a default value; try it as an int
            try:
                n = int(s)
            except ValueError:
                print("@@@@@ Not even an integer! @@@@@")
                continue
            # works as an int; is it valid?
            if fn_validate(n):
                return n
    
    
    while kjonn not in ('m', 'd'):
        kjonn = input("Hvilken kjnn er du? (M)ann/(D)ame\t").strip().lower()
    
    alder = get_input_int("Hvor mange r er du?\t", check_positive)
    hvilepuls = get_input_int("Hva er din hvilepuls?\t", check_positive)
    makspuls = get_input_int("Hva er din makspuls?\t", check_positive_or_zero, default=0)
    
    
    if not makspuls:
        if kjonn == 'm':
            makspuls = int(214 - 0.8*int(alder))
        else:
            makspuls = int(209 - 0.7*int(alder))
    
    prosent60 = (makspuls-hvilepuls)*0.6 + hvilepuls
    prosent75 = (makspuls-hvilepuls)*0.75 + hvilepuls
    prosent85 = (makspuls-hvilepuls)*0.85 + hvilepuls
    prosent90 = (makspuls-hvilepuls)*0.9 + hvilepuls
    
    # Remove decimals
    prosent60 = round(prosent60)
    prosent75 = round(prosent75)
    prosent85 = round(prosent85)
    prosent90 = round(prosent90)
    
    
    print("\n\n")
    if kjonn == 'm':
        print("Din kjnn  er:\t\tMann")
    else:
        print("Din kjnn  er:\t\tDame")
    
    print("Din alder er:\t\t", alder)
    print("Din hvilepuls er:\t", hvilepuls," slag/min")
    print("Din makspuls er:\t", makspuls," slag/min")
    
    print("\n\n")
    print("Din lavsone er:\t",prosent60,"-",prosent75," slag/min")
    print("Din ATsone er:\t",prosent85,"-",prosent90," slag/min")
    print("Din VO2max er:\t",prosent90,"-",makspuls," slag/min")
    
    
    input("\n\nTrykk enter for avslutte")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a secure transfer file program using Python and AES and
I am trying to write a python program using Python 3 I have to
I am trying to write a program using python-fuse, but I can't get file
I am trying to re-write a dictionary program in python(3.X). I have been using
I am trying to write a python program that calculates a histogram, given a
I'm trying to write a program in Python and I'm told to run an
i have been trying to write a program in python to read two text
I'm currently trying to write a voicechat program in python. All tips/trick is welcome
I am trying to write a cross-platform python program that would run in the
I am using Python 3.0 to write a program. In this program I deal

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.