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)
2
  • the data type is different of answer and correct Commented Dec 16, 2017 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. Commented Dec 16, 2017 at 11:43

1 Answer 1

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)
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.