2

I am learning python. And I created a simple application related to loans. The problem statement is like this.

If the applicant has a high income and good credit, he/she is eligible for a loan. Else not. Here is the code

has_high_income = input("Is your income high (Y/N): ")
has_good_credit = input("Is your good credit (Y/N): ")

if has_high_income and has_good_credit == "Y":
    print("Eligible for the loan")
else:
    print("Not eligible for a loan")

If I do Y in both cases then it will show me Eligible for the loan which is correct. If I do 1st Y and the second N then it shows me Not eligible for a loan which is also correct but when I do 1st N and 2nd Y it shows me Eligible for the loan which should be not correct. Please tell me what I have done wrong in my code. I will be very happy if you will solve my problem!!

3 Answers3

0

You need to check if both variables are equal to "Y". Right now, you are checking if has_good_credit is equal to "Y", and if has_high_income is "true". Which it is even if it is equal to "N".

This is the correct code:

has_high_income = input("Is your income high (Y/N): ")
has_good_credit = input("Is your good credit (Y/N): ")

if has_high_income == "Y" and has_good_credit == "Y":
    print("Eligible for the loan")
else:
    print("Not eligible for a loan")

A string variable is true as long as its not empty. Since you are putting "N" in has_high_income, it is not empty and therefore, it is true.

That is why bool(has_high_income) will return True.

Shiverz
  • 663
  • 1
  • 8
  • 23
  • You're welcome, glad to help ! :) For added information, another way to this would be like this : `if all([has_high_income=="Y", has_good_credit=="Y"]):` but it is a way less known way, and you might want to stick to standard conventions for now. – Shiverz Nov 13 '20 at 10:55
0

You need to make sure that u check for both inputs not just one

When you had this code

if has_high_income and has_good_credit == "Y":
    print("Eligible for the loan")

This first check if has_high_income is being checked if it's True not if the content is == "Y"


has_high_income = input("Is your income high (Y/N): ")
has_good_credit = input("Is your good credit (Y/N): ")

if has_high_income == "Y" and has_good_credit == "Y":
    print("Eligible for the loan")
else:
    print("Not eligible for a loan")
0

In python, when you have a statement such as:

if has_high_income and has_good_credit == "Y":

You are checking two statements here:

if has_high_income:

and

if has_good_credit == "Y":

When you check an if statement without an equal sign, you are checking if that variable is not equal to zero, in other words, any non-empty value will return true. In the other case, it checks normally for the equation.

Another thing to keep in mind is that the "and" keyword turns them into two completely separate statements, in other words you cannot do something like:

if thisVariable > 10 and < 50:

but you will have to do:

if thisVariable > 10 and thisVariable < 50
Zaid Al Shattle
  • 1,454
  • 1
  • 12
  • 21