Minor information: I am using a Lenovo Chromebook (Linux), CodePad (Text Editor), and Terminal (as a way to see the output).
Objective: I wanted to create a game-like program, where the computer generates random values between 0 to a 100 and generates a random operator, to add/subtract/multiply/divide with another random value between 0 to a 100. If you get the question correct, your score goes up by 1 and if it's wrong your score decreases by 1.
Problem: The program works good on all the operators except division. I want the division answer to be rounded to the nearest hundredths. For example: 23 / 54 = 0.42592592592. I want the program to accept it as 0.43.
Here's what I have: (Can someone edit my code below to Python, I'm not sure how).
from random import randint
import random
score = 0
while True:
num1 = randint(0,100)
num2 = randint(0,100)
op = random.choice(["+", "-", "*", "/"])
if op == '+':
print(num1, "+", num2, "= x ")
x_answer = num1 + num2
elif op == '-':
print(num1, '-', num2, '= x')
x_answer = num1 - num2
elif op == '*':
print(num1, "*", num2, "= x")
x_answer = num1 * num2
elif op == "/":
print(num1, "/", num2, "= x")
x_answer = num1 / num2
else:
print("Invalid Operator")
x = float(input("What's x? "))
if x == x_answer:
score += 1
print("CORRECT, your score is:", score)
elif x != x_answer:
score -= 1
print("INCORRECT, you lost a point! Current score:", score)
Again, just to reiterate the problem: when there's a division question, the answer has to be the entire thing (example: 0.432...) I want the computer to accept 0.43 as the answer and let the score go up if 0.43 is the answer.