[text] Unlock

Viewer

  1. '''
  2.  
  3.  
  4.   promptForInteger: 
  5.   request the user of integer input and validates the input whether it is a integer and lies between given bounds
  6.   @lower: lower bound for user input prompt
  7.   @upper:upper bound for user input prompt
  8.   @return: returns input entered by user after validation
  9.  
  10.  
  11. '''
  12. def promptForInteger(lower,upper):
  13.   ## loop until the user enters the right value
  14.   while True:
  15.     ## validate whether input is integer
  16.     try:
  17.       weight = int(input())
  18.     except ValueError:
  19.       print("Oops!  That was no valid integer.  Try again...")
  20.     ## check for bounds
  21.     if weight>=lower and weight<=upper:      
  22.       break
  23.     else:
  24.       print("Value was out of bounds")
  25.   return weight
  26. '''
  27.  
  28.  
  29.   promptForMorF: 
  30.   request the user of integer input and validates the input whether it is valid input for gender
  31.  
  32.  
  33.   @return: returns gender input entered by user after validation
  34.  
  35.  
  36. '''
  37. def promptForMorF():
  38.   ## loop until valid input
  39.   while True:
  40.     gender = input()
  41.     ## check whether it is M or F
  42.     if gender=="M" or gender=="F":
  43.       
  44.       break
  45.     else:
  46.       print("You must enter M or F")
  47.   return gender
  48. '''
  49.  
  50.  
  51.   computeBloodAlcoholConcentration: 
  52.   computes blood alchohol concentration using drinks,weight and duration
  53.   @drinks:no of drinks 
  54.   @weight:weight of the person
  55.   @duration:duration after taking alcohol
  56.   @return: returns bac
  57.  
  58.  
  59. '''
  60. def computeBloodAlcoholConcentration(drinks, weight, duration):
  61.   ba_inital = drinks/weight ## calclate initial bac at 0 duration
  62.   ## multiply constants for male and female.Calculate bac at the specific duration
  63.   bac_male = (ba_inital*3.8)-((duration//40)*0.01)
  64.   bac_female = (ba_inital*4.5)-((duration//40)*0.01)
  65.  
  66.  
  67.   return (bac_male,bac_female)
  68. '''
  69.  
  70.  
  71.   impairement: 
  72.   returns turns a string with the appropriate message for the blood alcohol concentration.
  73.   @bac:blood alcohol concentration 
  74.   @return: returns the appropriate message for given bac
  75.  
  76.  
  77. '''
  78. def impairement(bac):
  79.   ## print appropiate message based on given bac
  80.   if bac==0:
  81.     return "Safe to drive"
  82.   elif bac>0 and bac<=0.04:
  83.     return "Some Impairment"
  84.   elif bac>0.04 and bac<=0.08:
  85.     return "Driving Skills Significantly Affected"  
  86.   elif bac>0.08 and bac<=0.1:
  87.     return "Criminal Penalties in Most US States"  
  88.   elif bac>=0.1 and bac<0.3:
  89.     return "Legally Intoxicated - Criminal Penalties in All US States"
  90.   elif bac>=0.30:
  91.     return "Death is possible"
  92. '''
  93.  
  94.  
  95.   showImpairmentChart: A function that receives the weight, duration and biological sex and displays an impairment chart similar to the examples given below.
  96.   .
  97.   @weight:weight of the Person 
  98.   @duration:duration since last drink
  99.   @sex:gender of the person 
  100.   
  101.  
  102.  
  103. '''
  104. def showImpairmentChart(weight,duration,sex):
  105.   ## loop through 11 drinks from 0 to 11
  106.   gender ="male" if sex=="M" else "female"
  107.   print("")
  108.   print("")
  109.   print(f"{weight} pounds,{gender},{duration} minutes since last drink")
  110.   print("")
  111.   for i in range(12):
  112.     # take first value from tuple returned from computeBloodAlcoholConcentration if male else take second value of tuple
  113.     bac = computeBloodAlcoholConcentration(i,weight,duration)[0] if sex=="M" else computeBloodAlcoholConcentration(i,weight,duration)[1]
  114.     ## get appropiate message
  115.     chart = impairement(bac)
  116.     ## print in the given format
  117.     print(str(i)+" ",end="") if len(str(i))<=1 else print(str(i),end="")
  118.     print(" "*5,end="")
  119.     print("%.4f"%bac,end="")
  120.     print(" "*3,end="")
  121.     print(chart)
  122.   
  123.   
  124.  
  125.  
  126.  
  127. if __name__ == '__main__':
  128.  
  129.  
  130.   print('Enter your weight (in lbs): ')
  131.  
  132.  
  133.   weight = promptForInteger(0, 500)
  134.  
  135.  
  136.   print('How many minutes has it been since your last drink? ')
  137.  
  138.  
  139.   duration = promptForInteger(0, 300)
  140.  
  141.  
  142.   print('Enter your sex as M or F: ')
  143.  
  144.  
  145.   sex = promptForMorF()
  146.  
  147.  
  148.   showImpairmentChart(weight, duration, sex)

Editor

You can edit this paste and save as new:


File Description
  • Unlock
  • Paste Code
  • 25 Feb-2021
  • 3.86 Kb
You can Share it: