这是我的完整代码:
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的结果如何,这个问题在第三次运行时都会一致地重复,并且总是以数字999999999999995结束。
答案 0 :(得分:1)
无法显示浮动数字。
尝试使用格式
"{0:.1f}".format(currency)