1

So I am not great with Python if it is not obvious, but I can't seem to figure out why it is straight up ignoring my if statement. Could anyone please shed some light on this, I have double checked my variable names.

I have more code above this that imports random and does some password checking successfully, but this section of code isn't working as expected.

correct=0
q1a=random.randint(0,10)
q1b=random.randint(0,10)
answer1=q1a+q1b

print(q1a, "+", q1b, "=")
a1=input("Answer: " )

if answer1==a1:
    print("Correct!")
    correct+=1

print(answer1,a1,correct)```
J B
  • 13
  • 2

1 Answers1

1

Typecast input to integer, by default it takes it as a string.

a1 = int(input("Answer:"))
Siddharth Das
  • 1,057
  • 1
  • 15
  • 33
  • @tdelaney - This already answers the question (which is also a very popular preexisting question). Debugging unrelated issues is beyond the scope of a single question. – TigerhawkT3 Dec 09 '20 at 01:52
  • @TigerhawkT3 - What does "typecast" mean - as a C programmer I might assume `a1 = (int) input("Answer: ")`. Its trivial to show an example. As for the other bug, sure, its not necessary to fix in the answer, but again, trivial. Adding code examples is preferred in questions and answers. – tdelaney Dec 09 '20 at 01:59
  • @tdelaney - Why would you be assuming things as a C programmer in the Python tag? In theory, readers in a tag will have at least looked at a tutorial, opened their textbook, etc. – TigerhawkT3 Dec 09 '20 at 02:11
  • @TigerhawkT3- clearly I used C as an example that "typecast" could use additional explanation. And that added example looks pretty good to me. People who already know how to do type conversion in python wouldn't be looking to this solution in the first place. Its okay for non-experts to look at answers too. – tdelaney Dec 09 '20 at 02:32
  • @tdelaney - Actually, "Teach me this basic language feature" is off-topic for Stack Overflow. Stack Overflow is not intended to replace existing tutorials and documentation. – TigerhawkT3 Dec 10 '20 at 19:48