- '''
- promptForInteger:
- request the user of integer input and validates the input whether it is a integer and lies between given bounds
- @lower: lower bound for user input prompt
- @upper:upper bound for user input prompt
- @return: returns input entered by user after validation
- '''
- def promptForInteger(lower,upper):
- ## loop until the user enters the right value
- while True:
- ## validate whether input is integer
- try:
- weight = int(input())
- except ValueError:
- print("Oops! That was no valid integer. Try again...")
- ## check for bounds
- if weight>=lower and weight<=upper:
- break
- else:
- print("Value was out of bounds")
- return weight
- '''
- promptForMorF:
- request the user of integer input and validates the input whether it is valid input for gender
- @return: returns gender input entered by user after validation
- '''
- def promptForMorF():
- ## loop until valid input
- while True:
- gender = input()
- ## check whether it is M or F
- if gender=="M" or gender=="F":
- break
- else:
- print("You must enter M or F")
- return gender
- '''
- computeBloodAlcoholConcentration:
- computes blood alchohol concentration using drinks,weight and duration
- @drinks:no of drinks
- @weight:weight of the person
- @duration:duration after taking alcohol
- @return: returns bac
- '''
- def computeBloodAlcoholConcentration(drinks, weight, duration):
- ba_inital = drinks/weight ## calclate initial bac at 0 duration
- ## multiply constants for male and female.Calculate bac at the specific duration
- bac_male = (ba_inital*3.8)-((duration//40)*0.01)
- bac_female = (ba_inital*4.5)-((duration//40)*0.01)
- return (bac_male,bac_female)
- '''
- impairement:
- returns turns a string with the appropriate message for the blood alcohol concentration.
- @bac:blood alcohol concentration
- @return: returns the appropriate message for given bac
- '''
- def impairement(bac):
- ## print appropiate message based on given bac
- if bac==0:
- return "Safe to drive"
- elif bac>0 and bac<=0.04:
- return "Some Impairment"
- elif bac>0.04 and bac<=0.08:
- return "Driving Skills Significantly Affected"
- elif bac>0.08 and bac<=0.1:
- return "Criminal Penalties in Most US States"
- elif bac>=0.1 and bac<0.3:
- return "Legally Intoxicated - Criminal Penalties in All US States"
- elif bac>=0.30:
- return "Death is possible"
- '''
- showImpairmentChart: A function that receives the weight, duration and biological sex and displays an impairment chart similar to the examples given below.
- .
- @weight:weight of the Person
- @duration:duration since last drink
- @sex:gender of the person
- '''
- def showImpairmentChart(weight,duration,sex):
- ## loop through 11 drinks from 0 to 11
- gender ="male" if sex=="M" else "female"
- print("")
- print("")
- print(f"{weight} pounds,{gender},{duration} minutes since last drink")
- print("")
- for i in range(12):
- # take first value from tuple returned from computeBloodAlcoholConcentration if male else take second value of tuple
- bac = computeBloodAlcoholConcentration(i,weight,duration)[0] if sex=="M" else computeBloodAlcoholConcentration(i,weight,duration)[1]
- ## get appropiate message
- chart = impairement(bac)
- ## print in the given format
- print(str(i)+" ",end="") if len(str(i))<=1 else print(str(i),end="")
- print(" "*5,end="")
- print("%.4f"%bac,end="")
- print(" "*3,end="")
- print(chart)
- if __name__ == '__main__':
- print('Enter your weight (in lbs): ')
- weight = promptForInteger(0, 500)
- print('How many minutes has it been since your last drink? ')
- duration = promptForInteger(0, 300)
- print('Enter your sex as M or F: ')
- sex = promptForMorF()
- showImpairmentChart(weight, duration, sex)
[text] Unlock
Viewer
Editor
You can edit this paste and save as new: