0

I'm new to coding and have been trying to figure out why my code isn't working right.

credit_score = int(input('What is your credit score? '))
income = int(input('What is your annual income? '))
good_credit = credit_score>= 400
k = (20*50)

high_income = income>=250000
medium_income = high_income>income>= 70000
low_income = income<70000

if high_income and good_credit or medium_income and good_credit:
  loan = True
  if loan:
    loan = 'approved'
else:
  loan = 'not approved'
  

print(f'Your loan is {loan}')

The medium income variable falls between 70,000 and 250,000. What my code is supposed to do is return the string 'approved' when the inputted credit value is greater than or equal to 400 and the income falls between 70,000 and 250,000.

But every time I run it and put in the medium-income values it consistently returns 'not approved', which should only be returned if a low-income value is entered with a high credit value. However, if I enter a high credit value and a high-income value the code works just fine and returns 'approved'.

What is your credit score? 900
What is your annual income? 90000
Your loan is not approved?

With these values, it should return 'approved'

Any help is appreciated.

Asocia
  • 5,935
  • 2
  • 21
  • 46
  • Does this answer your question? [Priority of the logical statements NOT AND & OR in python](https://stackoverflow.com/questions/16679272/priority-of-the-logical-statements-not-and-or-in-python) – Pranav Hosangadi Jan 27 '21 at 14:49
  • 1
    `high_income` is a boolean, not a threshold to compare against `income`. You want `medium_income = not_high_income and income >= 70000`. – chepner Jan 27 '21 at 14:52
  • it returning "not approved" because ```high_income``` is False – Jailton Silva Jan 27 '21 at 14:52
  • @Thisbitchstoopid your condition can be simplified to `if good_credit and not low_income:` – AnkurSaxena Jan 27 '21 at 14:55

5 Answers5

0

In this line

medium_income = high_income>income>= 70000

Did you mean to put 250000? Because of that error, medium_income will be False for 90K also.

Also please get in habit of not using magic numbers.

lllrnr101
  • 2,288
  • 2
  • 4
  • 15
0

You should add some brackets to your if statement for readability and to ensure it does what you intended. As it stands, or will be evaluated first, so your conditions will be

high_income and (good_credit or medium_income) and good_credit

which isn't what you intended it to be.

Your calculation of medium_income also appears to be incorrect. It appears that you intended it to be

medium_income = 250000>income>= 70000
Kemp
  • 3,467
  • 1
  • 18
  • 27
0

You are incorrectly using the high_income object which is a boolean to compare for the mid_income. Also, consider adding brackets around the logical expression.

Try this working code:

credit_score = int(input('What is your credit score? '))
income = int(input('What is your annual income? '))
good_credit = credit_score>= 400
k = (20*50)

high_income = income >= 250000
medium_income = 250000 > income >= 70000
low_income = income < 70000
if (high_income and good_credit) or (medium_income and good_credit):
  loan = 'approved'
else:
  loan = 'not approved'

print(f'Your loan is {loan}')
Krishna Chaurasia
  • 8,924
  • 6
  • 22
  • 35
0

Here:

high_income = income>=250000
medium_income = high_income>income>= 70000
low_income = income<70000

You create high_income which is True or False depending on user input, then you use this in comparison

high_income>income>= 70000

which mean depending on user input is True>income>= 70000 or False>income>= 70000. True and False when used in numeric comparison does act as 1 and 0 thus they become 1>income>= 70000 or 0>income>= 70000 therefore medium_income is always False.

Daweo
  • 31,313
  • 3
  • 12
  • 25
0

You are treating high_income as a threshold against which income can be compared, not the boolean value that it is. Since high_income == False, your definition of medium_income becomes

medium_income = False > income >= 70000

This is legal, because bool is a subclass of int and False == 0. As a result, medium_income is False, because 0 is not greater than income.

Instead, you want

medium_income = not high_income and medium_income >= 70000

If you want to store the thresholds, I suggest something like

 high_income = 250_000
 medium_income = 70_000

 has_high_income = income >= high_income
 has_medium_income = high_income > income >= medium_income
 has_low_income = medium_income > income

 has_good_credit = credit_score >= 400

# if has_good_credit and (has_high_income or has_medium_income):
if has_high_income and has good_credit or has_medium_income and has_good_credit:
    loan = 'approved'
else:
    loan = 'not approved'
chepner
  • 497,756
  • 71
  • 530
  • 681