过程重复三次后的响应异常

2024-03-29 06:30:44 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我的完整代码:

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")

我接受三次“Y”后得到的回答是:

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

我不明白为什么会发生这种情况,代码会被检查出来,并且无论RNG的结果如何,这个问题在第三次运行时始终重复,并且总是以number.999999995结束


Tags: toyouyouriscurrentdowincurrency