1

I'm trying to write a function the returns BAC for both male and female sexes. The formula for BAC for my assignment is BAC = (drinks / Weight * R) with R being a different value for males and females. How would I get it to return the value for both sexes?

This is the code I tried using but it doesn't work:

if sex =='M':
    R = 3.8
    BAC = (drinks / Weight * R) 
    BAC = BAC - (0.01 * (Duration / 40))
    return BAC
if sex == 'F':
    R = 4.5
    BAC = (drinks / Weight * R) 
    BAC = BAC - (0.01 * (Duration / 40))
    return BAC

I have another function in the code that does define sex, but putting that into the function seems to complicate things, so I think I need to remove the variable sex altogether, but I'm not sure where to go from there.

martineau
  • 119,623
  • 25
  • 170
  • 301
jrdn462
  • 21
  • 1

3 Answers3

2

Use a tuple as a return value:

# we're inside a function def here
    RM = 3.8
    BACM = (drinks / Weight * RM) 
    BACM = BACM - (0.01 * (Duration / 40))
    
    RF = 4.5
    BACF = (drinks / Weight * RF) 
    BACF = BAFC - (0.01 * (Duration / 40))

    return BACM, BACF
mozway
  • 194,879
  • 13
  • 39
  • 75
0

In python, returning 2 values is like returning a tuple (basically a list). This can be very useful, especially when mixed with destructuring a list. Example:

def get_bac():
    maleBAC = 50  # random number for now, replace with the math necessary
    femaleBAC = 75
    return maleBAC, femaleBAC


male, female = get_bac()
print(male)  # 50
print(female)  # 75
Scrapper142
  • 566
  • 1
  • 3
  • 12
0

This sounds like an xy problem. While the answer to your question is that you can return any collection type, like a tuple, Sequence, Mapping, or even create your own class, it is just simpler to accept the gender as a parameter to the function.

Abhijit Sarkar
  • 21,927
  • 20
  • 110
  • 219