0

I'm a beginner in Python and sorry if my question is too obvious. I'm trying to create a Quiz class and inside of it, I have a method that should count a number of correct answers and then print it out. But somehow it will always print the maximum possible number of correct answers (even if all of them are false). I'm sure the solusion is super easy but I'm stuck with it and can't find anything similar elsewhere. This is my counter method:

def check_answers(self, answers):
    correct_answers_counter = 0
    if answers[0].lower() == 'a' or '(a)':
        correct_answers_counter += 1
    if answers[1].lower() == 'c' or '(c)':
        correct_answers_counter += 1
    if answers[2].lower() == 'b' or '(b)':
        correct_answers_counter += 1
    self.show_results(correct_answers_counter)

and if student1.check_answers(['c', 'c', 'c']), it still shows Your score is 3/3

  • 1
    Go to interactive Python and type `'b' == 'c' or '(c)'`. Is the result what you expect? The `==` is evaluated before `or`, so you're really asking `('b' == 'c') or '(c)'`, which is always `'(c)'`. – jpf Sep 05 '21 at 12:32
  • 1
    In this situation I'd tend to use `answers[0].lower() in ['a','(a)']`. – jpf Sep 05 '21 at 12:38
  • Thanks! It's more convenient than answers[0].lower() == 'a' or answers[0].lower() == '(a)': – Haifisch Sep 05 '21 at 12:42

0 Answers0