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")
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 checkin ('m', 'd'), but because we are checking for a single character, we can actually just checkin "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
sfor “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 thewhile True:loop and add an explicitbreakto 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 seebreakinside 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 usedround(), 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: