 # Code which needs help

I am new to Python and need help for this code

if AHT > 600 then the score should be 1 else 0
if Aux > 17% then the score should be 1 else 0
if Hold > 60 then the score should be 1 else 0
if CPA < 33 then the score should be 1 else 0

Then after getting the values, we need to calculate the sum.

If the sum is 4, We call it as VVH
If the sum is 3, We call it as VH
If the sum is 2, We call it as MH
If the sum is 1, We call it as LH

1 Like

The exact code will depend on how you’re receiving input and how you’re supposed to output the answer, but here’s a general idea. Assume that `AHT`, `Aux`, `Hold`, and `CPA` are variables that are given to you and that you output the answer by printing a string.

This solution takes advantage of the fact that the Boolean values `True` and `False` are treated as `1` and `0` when added together.

``````score = (AHT > 600) + (Aux > 0.17) + (Hold > 60) + (CPA < 33)
if score == 4:
print('VVH')
elif score == 3:
print('VH')
elif score == 2:
print('MH')
elif score == 1:
print('LH')
``````
1 Like

Sum will be zero if all conditionals evaluate to False and what should happen then?

Some (alternative) ways to achieve desired result (avoiding chained if-s), assuming that score is calculated as provided by Aohan Dang:

``````values = ['LH', 'MH', 'VH', 'VVH']

# using enumerate
for i, value in enumerate(values, start=1):
if i == score:
print(value)
break

# using dictionary
mapping = dict(enumerate(values, start=1))
print(mapping[score])
``````

Neither these examples address 0-sum case.