'Printing both outcomes of if statements

I am a complete beginner in python and was trying to write a simple programme for deciding if you were eligible for a loan or not but regardless of my inputs both of the outputs are printed, I think it is an issue with my if statements?

response1=input('Do you have a high income?  ').lower()
if response1== 'yes':
    high_income=True
elif response1== 'no':
    high_income=False
response2=input('Do you have good credit?  ').lower()
if response2=='yes':
    good_credit=True
elif response2=='no':
    good_credit=False

if high_income or good_credit:False
print ('Sorry, you are not eligable for a loan!')

if high_income and good_credit:True
print('Congratulations you are eligable for a loan!')


Solution 1:[1]

When you put code immediately after :, it's treated as the body of the if statement. So your code is equivalent to:

if high_income or good_credit:
    False

print ('Sorry, you are not eligable for a loan!')

if high_income and good_credit:
    True

print('Congratulations you are eligable for a loan!')

As you can see, the print() statements are not controlled by the if conditions, so they're both printed unconditionally. The True and False lines don't actually do anything, but any expression is a valid statement.

What you want is:

if high_income and good_credit:
    print('Congratulations you are eligable for a loan!')
else:
    print('Sorry, you are not eligable for a loan!')

Note that I've also used else: rather than writing another if whose condition is the opposite of the first. This is less error-prone, since it can be confusing to invert complex conditions.

Solution 2:[2]

Here's the syntax you wanted:

if not high_income or not good_credit:
    print ('Sorry, you are not eligable for a loan!')

if high_income and good_credit:
    print('Congratulations you are eligable for a loan!')

Of course, in this case the second if could be replaced by an else:, but this shows you the syntax.

Here's another hint. This:

if response1== 'yes':
    high_income=True
elif response1== 'no':
    high_income=False

can be replaced by:

high_income = response1 == 'yes'

As it is, your code will explode if the user answers anything other than "yes" or "no" (modulo case), because high_income will not be defined.

Solution 3:[3]

Your code should look like this:

if not (high_income and good_credit):
    print('not eligible')
else:
    print('eligible')

Whenever code is in a block, it needs to be indented four spaces from the surrounding code. In addition, since your high_income and good_credit variables are booleans, you can simply use them without comparing them to True or False. And since there are only two options, whether they get the loan or not, you can use an if-else statement and remove the second condition.

Solution 4:[4]

Python is an Object Oriented Language and we need to treat it as such whenever it's possible. Therefore, I believe it's important to provide a complete answer than focusing only on a simple indentation mistake.

First, we need to decompose the problem into smaller problems, that can be easier to solve.

In practice, your program asks a series of questions and given the answers, it provides a final answer.

So, let's create a Question object.

class Question:
    def __init__(self, question: str, score: int = 0):
        self.question = question.lower()
        self.score = score
    
    def ask(self):
        response = input(self.question)
        return response == 'yes'
    
    def get_score(self):
        return self.score if self.ask() else -self.score

This object not only asks the user a question, but also it gives a score according to the answer, which is very useful in our case.

Then we need to create an object that evaluates a series of the object Question and give a final result.

class Questionnaire:
    def __init__(self, threshold: int):
        self.threshold = threshold
        self.questions = []
    
    def add_question(self, question: LoanQuestion):
        self.questions.append(question)
    
    def run_questionnaire(self):
        total_score = sum([q.get_score() for q in self.questions])
        if total_score >= self.threshold:
            print('Congratulations you are eligable for a loan!')
        else:
            print('Sorry, you are not eligable for a loan!')

What's the result?

question_1 = Question('Do you have a high income?', score=5)
question_2 = Question('Do you have good credit?', score=5)
question_3 = Question('Do you like pineapple pizza?', score=0)

questionnaire = Questionnaire(threshold=10)

questionnaire.add_question(question_1)
questionnaire.add_question(question_2)
questionnaire.add_question(question_3)

questionnaire.run_questionnaire()

Solution 5:[5]

Problem statement : The person is eligible to get loan from bank?
Condition : He/She should have high income and good credit, to get loan.

res1 = input("Do you have high income (yes/no): ")
res2 = input("Do you have good credit (yes/no): ")
if (res1 and res2) == 'yes':
   print("You are eligible to get loan")
else:
   print("You are not eligible to get loan")

Solution 6:[6]

You can use this. Print Congratulations only when both high income and high credit are true, otherwise print Sorry, not eligible.

response1=input('Do you have a high income?  ').lower()
if response1== 'yes':
    high_income=True
elif response1== 'no':
    high_income=False
response2=input('Do you have good credit?  ').lower()
if response2=='yes':
    good_credit=True
elif response2=='no':
    good_credit=False

if high_income and good_credit:
    print('Congratulations you are eligable for a loan!')
else:
    print ('Sorry, you are not eligable for a loan!')

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Barmar
Solution 2 Tim Roberts
Solution 3
Solution 4 Commissar Vasili Karlovic
Solution 5 Sriram
Solution 6 Dharman