0

This is my full code:

from random import randint
currency = float(5.0)
while currency > 0:
    start = input("Do you wish to gamble? Y/N ")
    if start == "Y":
        numbers = []
        currency = currency - 0.2
        di = randint (1,6)
        numbers.append(di)
        dice = randint (1,6)
        numbers.append(dice) 
        thrice = randint (1,6)
        numbers.append(thrice)
        print(numbers[0])
        print(numbers[1])
        print(numbers[2])
        if numbers[0] == numbers[1] or numbers[0] == numbers[2] or numbers[1] == numbers[2]:
            currency = currency + 1.0
            print("You win £1!","Your current balance is: £",currency)
        elif numbers[0] == numbers[1] and numbers[0] == numbers[2]:
            currency = currency + 2.0
            print("Jackpot! You win £2!","Your current balance is: £",currency)
        else:
            print("Too bad!","You're currency is: £",currency)
    elif start == "N":
        print("Your final total was: £",currency)
        break
    else:
        print("Invalid Response")

And the response I get after accepting as "Y" three times is:

Do you wish to gamble? Y/N Y
1
6
2
Too bad! You're currency is: £ 4.8
Do you wish to gamble? Y/N Y
2
4
2
You win £1! Your current balance is: £ 5.6
Do you wish to gamble? Y/N Y
1
1
6
You win £1! Your current balance is: £ 6.3999999999999995

I do not understand why this is happening, the code checks out, and this issue repeats consistently on the third run no matter the outcome of the RNG and will always end in number.999999999999995.

TwistedSim
  • 1,960
  • 9
  • 23

1 Answers1

1

floating number could not be presented.

try to using format

"{0:.1f}".format(currency)
galaxyan
  • 5,944
  • 2
  • 19
  • 43