转换为float时输入0不起作用?

时间:2015-12-06 13:23:36

标签: python python-3.x

我正在制作一个二十一点游戏的投注部分有问题,我希望玩家能够在他们想要的时候下注$ 0,但是当你输入0时它只是重复输入。请帮助,因为这是我已经工作了一段时间的成功游戏的最后一步。

money=250
validbet=False
while validbet==False:
    bet = None
    while not bet:
        unchecked_bet = input("How much would you like to bet? $")
        try:
            bet = float(unchecked_bet)
        except ValueError:
            print("Sorry, that is not a valid bet. Please enter a valid bet.".format(unchecked_bet))

    bet=float(unchecked_bet)

    if bet>=0 and bet<=money:
        validbet=True
    else:
        print("Sorry, that is not a valid bet. Please enter a valid bet.")
        validbet=False


print("You have bet: $"+str(bet))

提前致谢

1 个答案:

答案 0 :(得分:1)

while not bet
如果False

评估为bet == 0。您可能想要检查

while bet is None

更明确地处理None

请注意bet == None也可以。但bet is None更准确,是检查None的推荐方法。 (有关详细信息,请参阅this question。)