Advertisement
Guest User

BMR calculator

a guest
Dec 13th, 2016
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.58 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. from sys import argv, exit
  3.  
  4. def getinput(question,t):
  5.     inp = None
  6.     while True:
  7.         try:
  8.             inp = t(input(question))
  9.             return inp
  10.         except KeyboardInterrupt:
  11.             exit()
  12.         except:
  13.             print("please input the correct type")
  14.  
  15.  
  16. def lbtokg(lb):
  17.     return lb / 2.202
  18.  
  19. def inchtocm(inches):
  20.     return inches * 2.540
  21.  
  22. def harris_male(weight,height,age):
  23.     return 66+ 13.7*weight + 5*height - 6.8*age
  24.  
  25. def harris_female(weight,height,age):
  26.     return 655 + 9.6*weight + 1.8*height - 4.7*age
  27.  
  28. def mifflin_male(weight,height,age):
  29.     return 9.99*weight + 6.25*height - 4.92*age + 5
  30.  
  31. def mifflin_female(weight,height,age):
  32.     return 9.99*weight + 6.25*height - 4.92*age - 161
  33.  
  34. def katch(LBM):
  35.     return 370 + 21.6*LBM
  36.  
  37. def cunningham(LBM):
  38.     return 500 + 22*LBM
  39.  
  40. def main(metric=None,sex=None,weight=None,height=None,age=None,bf=None):
  41.     if metric == None:
  42.         metric = getinput("Metric units? y for yes, n for no  ",str)
  43.         if metric[0] == 'n' or metric[0] == 'N':
  44.             metric = 0
  45.         else: metric = 1
  46.     if sex    == None:
  47.         while sex not in ['m','M','f','F']:
  48.             sex    = getinput("Are you a male or female? (m or f) ", str)
  49.     if weight == None:
  50.         weight = getinput("What is your weight?   ",float)
  51.     if height == None:
  52.         height = getinput("What is your height?   ",float)
  53.     if age    == None:
  54.         age    = getinput("What is your age?      ",float)
  55.  
  56.     if metric == 0:
  57.         weight = lbtokg(weight)
  58.         height = inchtocm(height)
  59.  
  60.     if bf==None:
  61.         while True:
  62.             bf     = input("Optional: What is your body fat? ")
  63.             if bf == '':
  64.                 LBM=None
  65.                 break
  66.             else:
  67.                 try:
  68.                     LBM = weight * (1-(float(bf)/100))
  69.                     break
  70.                 except:
  71.                     print("please input it in correctly")
  72.     elif bf == 0.0:
  73.         LBM = None
  74.     else:
  75.         LBM = weight * (1-(bf/100))
  76.  
  77.  
  78.  
  79.     if sex[0] == 'm' or sex[0] == 'M':
  80.         print("Harris-Benedict: ",harris_male(weight,height,age))
  81.         print("Mifflin-St Jeor: ",mifflin_male(weight,height,age))
  82.     elif sex[0] == 'f' or sex[0] == 'F':
  83.         print("Harris-Benedict: ",harris_female(weight,height,age))
  84.         print("Mifflin-St Jeor: ",mifflin_female(weight,height,age))
  85.     if LBM:
  86.         print("Katch-McArdle:   ",katch(LBM))
  87.         print("Cunningham:      ",cunningham(LBM))
  88.  
  89.  
  90. if __name__ == '__main__':
  91.     if len(argv) != 1 and (argv[1] == '-h' or argv[1] == '--help'):
  92.         print(\
  93. """
  94. -w --weight   Set the weight
  95. -h --height   Set the height
  96. -a --age      set the age
  97. -s --sex      set the sex
  98. -m --metric   Use metric values
  99. -i --imperial Use Imperial/USA values
  100. -b --bodyfat  Used for the optional Katch-McArdle/Cunningham research methods
  101. --male        set the sex to male
  102. --female      set the sex to female
  103. """)
  104.         exit()
  105.  
  106.     weight = None
  107.     height = None
  108.     age    = None
  109.     sex    = None
  110.     metric = None
  111.     bf     = None
  112.  
  113.     index = 1
  114.     while index < len(argv):
  115.         if   argv[index] == '-w':
  116.             weight = float(argv[index+1])
  117.             index += 1
  118.         elif argv[index] == '--weight':
  119.             weight = float(argv[index+1])
  120.             index += 1
  121.         elif argv[index] == '-h':
  122.             height = float(argv[index+1])
  123.             index += 1
  124.         elif argv[index] == '--height':
  125.             height = float(argv[index+1])
  126.             index += 1
  127.         elif argv[index] == '-a':
  128.             age = float(argv[index+1])
  129.             index += 1
  130.         elif argv[index] == '--age':
  131.             age = float(argv[index+1])
  132.             index += 1
  133.         elif argv[index] == '-s':
  134.             sex = str(argv[index+1])
  135.             index += 1
  136.         elif argv[index] == '--sex':
  137.             sex = float(argv[index+1])
  138.             index += 1
  139.         elif argv[index] == '--male':
  140.             sex = 'M'
  141.         elif argv[index] == '--female':
  142.             sex = 'F'
  143.         elif argv[index] == '-m':
  144.             metric = 1
  145.         elif argv[index] == '--metric':
  146.             metric = 1
  147.         elif argv[index] == '-i':
  148.             metric = 0
  149.         elif argv[index] == '--imperial':
  150.             metric = 0
  151.         elif argv[index] == '-b':
  152.             bf = float(argv[index+1])
  153.             index += 1
  154.         elif argv[index] == '--bodyfat':
  155.             bf = float(argv[index+1])
  156.             index += 1
  157.         index+= 1
  158.     main(metric,sex,weight,height,age,bf)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement