1

This is to be a simple mathematics program for my child to help with arithmetic.

However, the IF statement does not work in Python3 but does in Python2!

Please may I ask what has changed (as I can't seem to find the answer)?

#import from library     
import random
import sys

#display the python version
print(sys.version)

#computer selects two random numbers between 1 and 10
x = random.randint(1,10)
y = random.randint(1,10)

#print x and y values
print("For x =", x, "and y =", y)

#calculate the correct answer to the sum of the two numbers and store it in the variable named "correct"
correct = x + y

#ask the user what is the sum of the two random numbers and provide a response
answer = input("What is the sum of x plus y ? ")
if answer == correct:
    print("well done")
else:
    print("Incorrect. The correct answer is", correct)
YSelf
  • 2,646
  • 1
  • 14
  • 19
seacole
  • 13
  • 2
  • the data type is different of answer and correct – babygame0ver Dec 16 '17 at 11:43
  • Look at the linked duplicate, `input` in python3 returns a string of the written input. `input` in python2 evaluated this as python code, returning an integer if an integer is written. – YSelf Dec 16 '17 at 11:43

1 Answers1

1

Data type of answer is string, After changing it to int it will work fine

import random
import sys

#display the python version
print(sys.version)

#computer selects two random numbers between 1 and 10
x = random.randint(1,10)
y = random.randint(1,10)

#print x and y values
print("For x =", x, "and y =", y)

#calculate the correct answer to the sum of the two numbers and store it in the variable named "correct"
correct = x + y

#ask the user what is the sum of the two random numbers and provide a response
answer = input("What is the sum of x plus y ? ")
if(int(answer)==correct):
    print("well done")
else:
    print("Incorrect. The correct answer is", correct)
babygame0ver
  • 447
  • 4
  • 16