微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

函数和随机化问题-初学者python

如何解决函数和随机化问题-初学者python

我正在解决这个问题,我必须创建一个黑杰克游戏。

问题1:在我同时输入player1和player2的show之前,我的解决方案似乎工作正常。它只是停止,不会继续使用show_cards函数以查看谁赢了……而且我不确定为什么,缩进在正确的位置并且函数的拼写是正确的。为什么不继续执行?

问题2:我使用popitem方法来在我在deal_cards函数中发牌时随机选择我的牌,但是,每次我开始游戏时,都会选择相同的牌。该游戏从来都不是二十一点的新游戏。如何获得新卡牌

GAME = 21
def main():
    deck = create_deck()
    deal_cards(deck)   
def create_deck():
    deck = {'Ace of Spades':1,'2 of Spades':2,'3 of Spades':3,\
           '4 of Spades':4,'5 of Spades':5,'6 of Spades':6,\
           '7 of Spades':7,'8 of Spades':8,'9 of Spades':9,\
           '10 of Spades':10,'Jack of Spades':10,'Queen of Spades':10,'King of Spades':10,'Ace of Hearts':1,'2 of Hearts':2,'3 of Hearts':3,'4 of Hearts':4,'5 of Hearts':5,'6 of Hearts':6,'7 of Hearts':7,'8 of Hearts':8,'9 of Hearts':9,'10 of Hearts':10,'Jack of Hearts':10,'Queen of Hearts':10,'Ace of Clubs':1,'2 of Clubs':2,'3 of Clubs':3,'4 of Clubs':4,'5 of Clubs':5,'6 of Clubs':6,'7 of Clubs':7,'8 of Clubs':8,'9 of Clubs':9,'10 of Clubs':10,'Jack of Clubs':10,'Queen of Clubs':10,'Ace of Diamonds':1,'2 of Diamonds':2,'3 of Diamonds':3,'4 of Diamonds':4,'5 of Diamonds':5,'6 of Diamonds':6,'7 of Diamonds':7,'8 of Diamonds':8,'9 of Diamonds':9,'10 of Diamonds':10,'Jack of Diamonds':10,'Queen of Diamonds':10}
    return deck
def deal_cards(deck):
    p1hand_value = 0
    p2hand_value = 0
    print()
    print('player1:')

    for count in range(2):
        card,value = deck.popitem()
        print(card)
        p1hand_value+=value
    print()
    print('player2:')    

    for count in range(2):
        card,value = deck.popitem()
        print(card)
        p2hand_value+=value
    
    p1_another_card(deck,p1hand_value,p2hand_value)

def p1_another_card(deck,p1hand,p2hand):
    ask = input("Would you like another card player1? If yes enter 'yes' if not enter 'show'")
    ask=ask.lower()

    while ask == 'yes':
        card,value = deck.popitem()
        print(card)
        p1hand+=value
    
        if p1hand < GAME:
            ask = input("would you like another card? If yes enter 'yes' if not enter 'show'") 
            ask = ask.lower()
        else:
            ask = False

    p2_another_card(deck,p2hand)         
        
def p2_another_card(deck,p2hand):

    ask = input("Would you like another card player2? If yes enter 'yes' if not enter 'show'")
    ask=ask.lower()

    while ask == 'yes':
        card,value = deck.popitem()
        print(card)
        p2hand+=value
    
        if p2hand < GAME:
            ask = input("would you like another card? If yes enter 'yes' if not enter 'show'") 
            ask = ask.lower()
        else:
            ask = False

    show_cards(p1hand,p2hand)
        
def show_cards(p1hand,p2hand):

    if p1hand > GAME and p2hand < GAME:
        print('player 2 hand:',p2hand,'Winner')
        print('player 1 hand:',"Busted")
    elif p1hand > GAME and p2hand > GAME:
        print('No one won. Both busted')
        print('player1:',p1hand)
        print('player2:',p2hand)
    elif p1hand < GAME and p2hand > GAME:
        print('player 1 hand:','Winner')
        print('player 2 hand:','Busted')
    elif p1hand < GAME and p2hand < GAME:
        diff1 = GAME-p1hand
        diff2 = GAME-p2hand
    
        if diff1 < diff2:
            print('player 1 hand:','winner')
        elif diff1 > diff2:
            print('player 2 hand:','Winner') 
    else:
        print('something went wrong')       
    
main()   
    

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。