【发布时间】:2018-02-08 21:29:06
【问题描述】:
在 truecount() 中,无论我将 float() 放在哪里,当它应该给我大约 0.505 时,它总是返回 1.0。我知道这可能与我格式化所有内容的方式有关,但对于我做错的任何帮助将不胜感激。 p.s.我对编程很陌生
def count():
global totalCards
global cardCount
card = int(raw_input("input card. "))
totalCards = totalCards - 1
if card == 1:
print "card invalid"
else:
if card <= 6 and card > 1:
cardCount = cardCount + 1
elif card == 10:
cardCount = cardCount - 1
elif card >= 7 and card <= 9:
cardCount = cardCount
return
def truecount():
global cardCount
global truecardCount
global totalCards
global decks
decksRemaining = float(totalCards/52)
truecardCount = float(cardCount / decksRemaining)
return
def main():
run = True
totalCards = 0
cardCount = 0
truecardCount = 0.0
while run == True:
print "Welcome to my card counter. Start using when dealer
shuffles."
user_input = raw_input("Type R When you're ready to start. Type X if
you want to quit.")
if user_input.upper() == "R":
global truecardCount
truecardCount = 0
global cardCount
cardCount = 0
runCount = True
decks = int(raw_input("What is the amount of decks in the shoe?
"))
global totalCards
totalCards = decks * 52
while runCount == True:
count()
truecount()
print(float(truecardCount))
elif user_input.upper() == "X":
run == False
else:
【问题讨论】:
-
将
float应用到除法的结果不会使除法不是浮点除法。它只是让你得到你本来会得到的整数,但表示为一个浮点数。 -
用
totalCards/52.0代替float(totalCards/52)。 -
在 Py3 中
division处理更改,因此您可以使用__future__,例如:from __future__ import division来提出 Py3 行为。
标签: python python-2.7 types floating-point