Convert negative results from the list into 0 in Python

Question:

I’ve done a program which counts income per family member and gives a result of funding.
There is a problem when one of the family member’s "earns negative value" (has a loss),
I want it to count negative values as 0.

Example of right answer:

Family members: 4
Children in family: 2
Input family member income (yearly): 414575
Input family member income (yearly): -12500
Input family member income (yearly): 0
Input family member income (yearly): 0
(Monthly) income per person: 8636.98
Amount of funding: 3200

My result is

a = int(input('Family members : '))
b = int(input('Children in family '))
income = c = ('Input family member income YEARLY : ')
stop = "Wrong Data."
if a < b and a <= 0 or b < 0:
    print(stop)
elif a == b:
    print(stop)
else:
    lst = []
    for n in range(a):
        incomes = float(input(c))
        lst.append(incomes)
    g = round(sum(lst) / (12 * a), 2)
    z = print("MONTLY income per person: ", g)
    if g < 1500:
        print("Amount of funding: ", (800 * b) + (1200 * (a - b)))
    elif g >= 1500:
        print("Amount of funding: ", (500 * b) + (1100 * (a - b)))
    elif g > 2500:
        print("Amount of funding: ", (300 * b) + (900 * (a - b)))

I’ve tried IF function

if g < 0:
        g = 0

but it only counts it as 0 when whole family income is negative (when sum is < 0), and I need it to count every inputed negative income as 0.

Asked By: Matty Cash

||

Answers:

If I understood the problem, just append the income only if it is >0. So:

for n in range(a):
        incomes = float(input(c))
        if incomes > 0:
           lst.append(incomes)
Answered By: en3rgizer

Have you tried doing it with the variable incomes?
If you say:

if incomes < 0: 
    incomes = 0

So then as soon as the input gets added to the list, it gets counted as zero if its value is negative?

Answered By: Flavio

The best approach is to use a conditional comprehension, as suggested by @Swifty, in the comments.

Here is your code fixed and using more descriptive variable names:

members = int(input('Family members: '))
children = int(input('Children in family: '))
adults = members - children

if members < children and members <= 0 or children < 0:
    print('Wrong data.')
elif members == children:
    print('Wrong data.')
else:
    incomes = []
    for _ in range(adults):
        incomes.append(float(input('Input family member income YEARLY: ')))

    monthly_income = round(
        sum(income for income in incomes if income > 0) / (12 * members), 2
    )

    print('MONTHLY income per person:', monthly_income)

    if monthly_income < 1500:
        print('Amount of funding:', (800 * children) + (1200 * adults))
    elif 1500 <= monthly_income < 2500:
        print('Amount of funding:', (500 * children) + (1100 * adults))
    else:
        print('Amount of funding:', (300 * children) + (900 * adults))

Using the input values from your question, the code above will result in the following output:

Family members: 4
Children in family: 2
Input family member income YEARLY: 414575
Input family member income YEARLY: -12500
Input family member income YEARLY: 0
Input family member income YEARLY: 0
MONTHLY income per person: 8636.98
Amount of funding: 2400

You can also use an easier logic (at least in IMHO), and ask for the income value only for the adults. Something like this:

members = int(input('Family members: '))
children = int(input('Children in family: '))
adults = members - children

if members > children and adults > 0 and children >= 0:
    incomes = [
        float(input('Input family member income YEARLY: ')) for _ in range(adults)
    ]

    monthly_income = round(
        sum(income for income in incomes if income > 0) / (12 * members), 2
    )

    print('MONTHLY income per person:', monthly_income)

    if monthly_income < 1500:
        funding = children * 800 + adults * 1200
    elif 1500 <= monthly_income < 2500:
        funding = children * 500 + adults * 1100
    else:
        funding = children * 300 + adults * 900

    print('Amount of funding:', funding)
else:
    print('Wrong data.')

Which will output the following, using the input values from your question:

Family members: 4
Children in family: 2
Input family member income YEARLY: 414575
Input family member income YEARLY: -12500
MONTHLY income per person: 8636.98
Amount of funding: 2400
Answered By: accdias
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.