0

I have created a for loop which is supposed to show me a question and, if replying correctly, it gives me 1 point. When I run the code, I see the questions correctly but when giving the correct answer to all questions, the result is only 1.

In the code below I have pasted the class used to define the object used in the loop and the loop which doesn't give me the total score.

It seems that the first and second questions if answered correctly, do not provide points, while the last question where the answer is "o", gives point

Classe.py

class Dom_matematica:
    def __init__(self, operazione, risultato):
        self.operazione = operazione
        self.risultato = risultato

Survey

from Classe import Dom_matematica 

Calcoli = [
    "Quest1\n",
    "Quest2\n",
    "Quest3\n",
]


Ogg_Calcoli = [
    Dom_matematica(Calcoli[0], 4),
    Dom_matematica(Calcoli[1], 9),
    Dom_matematica(Calcoli[2], "o"),
]


#HERE IS WHERE THE PROBLEM ARISES. 
def Loop_mat(Ogg_Calcoli):
    score = 0
    for d in Ogg_Calcoli:
        risposta = input(d.operazione)
        if risposta == d.risultato:
            score += 1
    print("You got " + str(score) + "/" + str(len(Ogg_Calcoli)) + " 
correct")

Loop_mat(Ogg_Calcoli)

I expect that the result gives 3/3 when all answers are correct

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
TheSeller
  • 5
  • 1
  • if you change `Dom_matematica(Calcoli[0], 4), Dom_matematica(Calcoli[1], 9),` to `Dom_matematica(Calcoli[0],'4'), Dom_matematica(Calcoli[1], '9'),` your code will work, issue arrise in answer which you seeking and which is mentioned are in different format . so keep format same and it is good to go – sahasrara62 Jun 14 '19 at 08:49

1 Answers1

0

The summation is OK.

The input() function always returns a string in Python 3. Therefore, your rispostas are actually "4", "9", and "o", thus only the last one is correct.

Integers are not their string representation, see e.g. this question.

dedObed
  • 1,313
  • 1
  • 11
  • 19
  • Thank you, but the preferred way of "thank you" is to accept when the answer works for you ;-) And upvote if you like it. – dedObed Jun 15 '19 at 09:27
  • 1
    So I did upvote but I don't have yet the points to have my vote showcased. Regarding the "accept answer", now I have learnt something new :D – TheSeller Jun 16 '19 at 23:24