Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

# Define a function to calculate the blood group

def calculate_blood_group(parent1_bg, parent2_bg, grandparent1_bg, grandparent2_bg):

# A person can have either blood group A or B if one of their parents has blood group A or B

if parent1_bg == 'A' or parent2_bg == 'A':

blood_group = 'A'

elif parent1_bg == 'B' or parent2_bg == 'B':

blood_group = 'B'

# If neither parent has blood group A or B, the person can have blood group O

else:

blood_group = 'O'

# If the person's blood group is A or B, they can also be positive or negative based on their
grandparents' blood groups

if blood_group == 'A' or blood_group == 'B':

if grandparent1_bg == '+' or grandparent2_bg == '+':

blood_group += '+'

else:

blood_group += '-'

# If the person's blood group is O, they can only be positive or negative based on their parents' blood
groups

else:

if parent1_bg == '+' or parent2_bg == '+':

blood_group += '+'

else:

blood_group += '-'

return blood_group

# Test the function with some example inputs


print(calculate_blood_group('A', 'B', '+', '+')) # should print "A+"

print(calculate_blood_group('A', 'O', '+', '-')) # should print "A+"

print(calculate_blood_group('B', 'O', '-', '+')) # should print "B-"

print(calculate_blood_group('O', 'O', '-', '-')) # should print "O-"

You might also like