无法弄清楚为什么它一直在运行我的循环

时间:2015-03-16 10:32:14

标签: python

我是python的初学者。我写了一个小程序,玩游戏轮盘赌(从赌场),但它是一个简化版本。

我无法弄清楚为什么它在我尝试时会继续运行错误的循环。当我“下注”它继续运行循环但我不知道为什么?

from math import ceil
from random import randrange

print '=================='
print '    Roulette v2   '
print '=================='

# init vars
bet = 0
pick = 0
restart = 1
paire = 0
keep = True
bille = randrange(50)

wallet = raw_input('Whats in your wallet : ') # ask the player how much money he gets for this session

try:
    wallet = int(wallet) # set wallet as integer, also test if user wrote a number
except ValueError:
    print '[!] It must be a number' # if not a number set a default value for wallet
    print '[!] Default value set to 50'
    wallet = 50 # 50 is our default value

while keep and restart: # while keep is true and restart equal 1,then keep playing

    print 'Wallet : ',wallet,'$' # show how much money we got left
    bet = raw_input('Place a bet : ') 

    while type(bet) != int: # same as above, check if bet is an integer

        try:
            bet = int(bet)
        except ValueError:
            print '[!] It must be a number'

        bet = raw_input('Place a bet : ') # if not, ask again

        while bet > wallet: # if not enough money then cancel bet
            print '[!] You do not have ',bet,'$ in your wallet !'
            print 'Your wallet : ',wallet
            bet = raw_input('Place a bet : ')

    pick = raw_input('Pick a number between 1-50 : ')

    while type(pick) != int: # same as above

        try:
            pick = int(pick)
        except ValueError:
            print '[!] It must be a number'

        pick = raw_input('Pick a number between 1-50 : ')

    print '...'
    print '....'
    print '.....'
    print '......!'

    if pick != bille: # if players number is different than random integer
        paire = pick % 2 # check if players number is pair

        if paire == 1: # if its pair then add bet + bet / 2 to his wallet
            print 'Your number is pair !'
            bet = bet + bet / 2
            ceil(bet) # we dont want a float number
            wallet = wallet + bet
            print 'You won : ',bet

        elif paire == 0: # else if number is odd, player loose his money
            print 'Your number is odd !'
            wallet = wallet - bet

    elif pick == bille: # if players number equal to random integer
        print 'J A C K P O T !!!'
        bet = bet * 3
        wallet = wallet + bet # then bet * 3 and add to players wallet
        print 'You won : ',bet

    if wallet < 0: # if players wallet empty then end of the game
        print '[!] You have no money left !'
        print '[!] You are out of the game !'
        keep = False # loop stop if keep isnt true
        restart = 0

    elif wallet > 0: # if player has no money left in his wallet
        restart = raw_input('Keep playing [o/n] ? ')

        try:
            str(restart)
        except TypeError:
            print '[!] Wrong answer !'

        while type(restart) != str:
            print '[!] Wrong answer !'
                        restart = raw_input('Keep playing [o/n] ? ')
            str(restart)

        if type(restart) == str:

            if restart == 'o':
                restart = 1

            elif restart == 'n':
                restart = 0
                Keep = False

        elif type(restart) != str:
            print '[!] Wrong answer !'





print '[!] Bye...' # just a test

1 个答案:

答案 0 :(得分:1)

你要求第二次下注,但是你忘了把它投入int然后你比较并循环(while bet > wallet)。

问题在于,在带有标准CPython解释器的Python 2中,这种比较总是True,因为比较不同类型的对象只会比较类型(和str > int);有关此实施细节,请参阅comment

然而,在Python 3中不再是这种情况,而且会产生TypeError: unorderable types: str() > int()异常,这会使您的问题更加清晰。