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()
print()
statements aren't inside theif
blocks. – Barmarif
.:False
and:True
don't do what you think they do. – Barmarif
statements where the code to run is after:
on the same line, instead of indented on the next line. – Barmar