samedi 9 avril 2016

I want to equal an user input to an answer and print according to user input

I am creating a math test program that creates random questions and I ask the user to input the answer and the answer is checked with the correct answer and the output is given.

import random
q = 1
live=3
operations = ("+" , "-")
for q in range(0,11):
    while live>=1:
        op=random.choice(operations)
        num1=random.randint(0,10)
        num2=random.randint(1,10)
        if op =="+":
            op=random.choice(operations)
            num1=random.randint(0,10)
            num2=random.randint(1,10)
            correct = num1+num2
            print("What is", " " +str(num1) + "+" +str(num2) + "?")
            answer =input("\n Enter answer")
            if answer==correct :
                print("Correct answer")
                q= q+1
            else:
                answer != correct
                live= live - 1
                print("The answer is incorrect. Try again mate!")

this is my incomplete code. The problem is even when there is a question with the addition mark and the user inputs the correct answer the programs still prints the wrong output (the output that should be printed if the user enters a wrong answer) Please, any help would be appreciated. Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire