mercredi 22 septembre 2021

Python - if statement always does instructions in else no matter what [duplicate]

My Python script always does the code in the "else" part of my if statement no matter what (note: i'm making a guessing game and here's how it works: it asks you a random math question and then checks if the math question is right, if it's right, it prints "correct" if it's wrong, then it prints "incorrect") here's my code:

import random


num1 = random.randrange(1, 10)
num2 = random.randrange(1, 10)
operator_selections = ["+", "-", "*", "/"]
operator_index = random.randrange(0, 3)
trf_answer = 0


def main():
    question = input("What is " + str(num1) + " " + operator_selections[operator_index] + " " + str(num2) + "? ")
    global trf_answer
    # Operator dilemma (idk why i commented this but anyway)
    if operator_index == 0:
        trf_answer = num1 + num2
    elif operator_index == 1:
        trf_answer = num1 - num2
    elif operator_index == 2:
        trf_answer = num1 * num2

    if question == trf_answer:
        print("Correct!")
    else:
        print("Incorrect!")


main()

Aucun commentaire:

Enregistrer un commentaire