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
  • 112,593
  • 23
  • 157
  • 280
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
  • 81,317
  • 8
  • 19
  • 49
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
  • 476
  • 1
  • 2
  • 10
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
  • 19,114
  • 16
  • 94
  • 178