LukeP412
LukeP412

Reputation: 13

Sum two numbers

When I type the input of the sum of number1 + number2 as "answer" and number1, number2 are two randomized numbers between 1-500, I get my "Wrong, sorry!" statement instead of the "Right!" when the answer is correct.

For example, if the two numbers are 479 + 121, the answer should be 600, right? Well, apparently it's not, my code likes to say. Due to some error logic, I'll guess. (Note this is just an excerpt, obviously. The variables were declared as integers before in the main module and random is imported.)

  def getNumbers():
     number1 = random.randint(1, 500)
     number2 = random.randint(1, 500)
     return number1, number2

  def getAnswer(number1, number2, answer):
     print("What is the answer to the following equation:")
     print(number1)
     print("+")
     print(number2)
     answer = input("What is the sum? ")
     return answer

 def checkAnswer(number1, number2, answer, right):
     if answer == number1 + number2:
       print("Right!")
     else:
       print("Wrong, sorry!")

It comes out as:

What is the answer to the following equation?

479

+

121

What is the sum? 600

Wrong, sorry!

0 also comes in as incorrect, so I'm not sure what the value is being set to. Any idea how to fix this code?

Upvotes: 1

Views: 3369

Answers (1)

ᴀʀᴍᴀɴ
ᴀʀᴍᴀɴ

Reputation: 4538

input function returns string , you should cast it to int :

answer = int(input("What is the sum? "))

because you didn't cast it to int "600" == 600 is always False and Wrong printed .

Upvotes: 3

Related Questions