Reputation: 23
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.
Upvotes: 1
Views: 775
Reputation: 5871
# this is a unit test for a comparison function f
def test(f):
# check the case where it should be rounded down
expected = 0.432
correct = 0.43
wrong = 0.44
if not f(expected, correct):
raise ValueError
if f(expected, wrong):
raise ValueError
# check the case where it should be rounded up
expected = 0.437
correct = 0.44
wrong = 0.43
if not f(expected, correct):
raise ValueError
if f(expected, wrong):
raise ValueError
# need to accept more precision if it's right
if not f(expected, expected):
raise ValueError
print ('passed')
places = 2
# floating point comparison by delta
def equivalent_delta(x, y):
delta = 5 * 10 ** - (places+1)
return abs(x-y) < delta
# comparison using builtin function round
def equivalent_round(x, y):
return round(x, places) == round(y, places)
# compare their string representation with 2 places
def equivalent_print(x, y):
format_string = '{{:.{}}}'.format(places)
return format_string.format(x) == format_string.format(y)
# multiply by 100, convert to integer (with 0.5 for rounding)
def equivalent_as_int(x, y):
return int(x * 10**places + 0.5) == int(y * 10**places + 0.5)
test(equivalent_delta)
test(equivalent_round)
test(equivalent_print)
test(equivalent_as_int)
see also Is floating point math broken?
Upvotes: 0
Reputation: 286
You can try the built in round()
function in python around the float value, like so:
elif op == "/":
print(num1, "/", num2, "= x")
x_answer = round(float(num1 / num2), 2)
The first value in the round()
function gives the value to round and the second tell python to what decimal place. In this example you want to the 2 decimal, so put 2.
Upvotes: 1