1

I'm pretty new to Python and I'm getting a weird error when I run a division problem. The overall program is larger, but the relevant part is:

                    problemNumber = 0
                    gamesWon = 0
                    gamesLost = 0
                    gameNumber = 0
                    percentageWon = 0
                    answer = 0.00
                    while problemNumber < 10:
                            numberOne = random.randrange(0,10,1)
                            numberTwo = random.randrange(1,10,1)
                            print("Answer the following question: ", numberOne, program, numberTwo)
                            response = float(input())
                            if program == "+":
                                    answer = numberOne + numberTwo
                            elif program == "-":
                                    answer = numberOne - numberTwo
                            elif program == "x":
                                    answer = numberOne * numberTwo
                            elif program == "/":
                                    answer = float(format(answer, '.2f'))
                                    answer = numberOne / numberTwo
                            elif program == "^":
                                    answer = numberOne ^ numberTwo
                            if response == answer:
                                    print(random.choice(congratulation_Message))
                                    gamesWon = gamesWon + 1
                                    problemNumber = problemNumber + 1
                            else:
                                    print(random.choice(admonishment_Message),"The answer was ", format(answer, '.2f'))
                                    gamesLost = gamesLost + 1
                                    problemNumber = problemNumber + 1

Most of it runs correctly, BUT division problems that round to 2 places come back as incorrect when they're identical to the answer:

Answer the following question:  3 / 2
1.5
Great job!
Answer the following question:  0 / 2
0
Great job!
Answer the following question:  8 / 7
1.14
Incorrecto, my friend.  The answer was  1.14
Answer the following question:  5 / 7
.71
Wrongo.  The answer was  0.71
Answer the following question:  1 / 9
0.11
AAAAH. Wrong.  The answer was  0.11

I think I have everything in the right format, but clearly something isn't quite right.

Any ideas?

Bips
  • 11
  • 2
  • You need to format the value *after* you compute the quotient, not before. It's irrelevant anyway, because the floating point value returned by `float` doesn't care about how many digits were present in the string used to initialize it. – chepner Oct 31 '18 at 17:16
  • Related question: https://stackoverflow.com/questions/588004/is-floating-point-math-broken – juanpa.arrivillaga Oct 31 '18 at 17:36

1 Answers1

-1

I think you need to format this one up to 2f like you did:

answer = numberOne / numberTwo

Because it's probably comparing

0.11 with 0.11111111