1

Starting off by saying I'm completely new to python, I have to do a school project and I'm stuck with this part of the code. Pretty much, the requirements are being met to go through with the IF statement (the user input is equal to the answer, I'm sure of that) and the system is skipping straight to ELSE. I'm not sure if I'm just blind and it's a simple formatting problem or if it's something else I'm missing.

while valid == False:
topic = input().lower()
if topic == "addition":
    print ("You have selected Addition!")
    valid = True

    ARand1 = randrange(1, 200)
    ARand2 = randrange(1, 200)
    question_answer = ARand1 + ARand2

    print ("Question",questions_asked,":")
    print ("What is",ARand1,"+",ARand2,"?")

    users_answer = input("Please input your answer")

    if users_answer == (question_answer):
        print ("Correct!")
        total_score += 1
        questions_asked += 1

    else:
        print ("Incorrect!")
        questions_asked += 1
        print (questions_asked)


elif topic == "subtraction":
    print ('You have selected Subtraction!')
    valid = True

    SRand1 = randrange(100, 200)
    SRand2 = randrange(1, 100)
    answer = SRand1 - SRand2

    print ("Question",questions_asked,":")
    print ("What is",SRand1,"-",SRand2,"?")

The part I'm getting stuck on is the second if statement (if users_answer == (question_answer):). It completely skips the IF and goes straight to the ELSE, even though the condition is met.

Apologies if it's something simple or if this poorly written, again I was just introduced to Python a few weeks ago.

DYZ
  • 55,249
  • 10
  • 64
  • 93
Sinnx
  • 29
  • 2

1 Answers1

2

change users_answer = input("Please input your answer") to users_answer = raw_input("Please input your answer") if the answer,is an int,parse it as int(raw_input("Please input your answer"))

Quoting this answer here input() :interprets and evaluates the input which means that if user enters integer,an integer will be returned ,if user enters string,string is returned.

raw_input():raw_input() takes exactly what user typed and passes it back as string .It doesn’t interprets the user input.Even an integer value of 10 is entered or a list is entered its type will be of string only.

Eliethesaiyan
  • 2,327
  • 1
  • 22
  • 35