PTA exercises [python] 6-8 jmu-python-dealing cards

Enter an integer from the keyboard as a random seed, randomly generate a deck of poker cards (remove the big and small kings), and distribute them to 4 players in a circular manner, each with 5 cards (the 1st, 5th, 9th, 13th, and 17th cards are given to the first player) Player, 2nd, 6th, 10th, 14th, 18th to the second player...and so on) and output.

Function interface definition:

create( ) 
shufflecard(pokers) 
deal(pokers,n) 

Among them, the function of create( ) is to generate a sequence of poker cards without king and king and return them; the function of shufflecard(pokers) is to randomly shuffle the cards and return the sequence of poker cards after shuffling, where the parameters passed in represent  pokers 52 A sequence of poker cards; deal(pokers,n) is to deal 5 cards to a player and output the cards dealt to that player (output "The i-th player's cards are: xx, xx, xx, xx, xx", where the colon is the Chinese symbol, and the comma between the 5 cards is the English symbol), which pokers is the sequence of 52 poker cards that have been shuffled,  n indicating the number of players.

Sample referee test procedure:

import random

/* 请在这里填写答案 */

suit=['♥','♠','♦','♣']
d=['A','2','3','4','5','6','7','8','9','10','J','Q','K']
n=int(input())
random.seed(n)
poker=create()
poker=shufflecard(poker)
for i in range(52):
    print('%-4s'%poker[i],end='  ')
    if i%13==12:
        print()
for i in range(1,5):
    deal(poker,i)

Input sample:

7

Sample output:

♠5    ♣A    ♦6    ♥J    ♣2    ♥Q    ♥A    ♠7    ♠2    ♣Q    ♠4    ♥9    ♦K    
♣6    ♦8    ♣7    ♠Q    ♦4    ♠10   ♥K    ♠9    ♣5    ♦5    ♦3    ♣J    ♣K    
♥8    ♣10   ♠6    ♦10   ♥2    ♦J    ♣4    ♠3    ♣8    ♦A    ♦2    ♥6    ♥3    
♠A    ♦7    ♣9    ♦Q    ♠J    ♥7    ♦9    ♥5    ♥4    ♣3    ♠K    ♥10   ♠8    
第1个玩家拿到的牌是:♠5,♣2,♠2,♦K,♠Q
第2个玩家拿到的牌是:♣A,♥Q,♣Q,♣6,♦4
第3个玩家拿到的牌是:♦6,♥A,♠4,♦8,♠10
第4个玩家拿到的牌是:♥J,♠7,♥9,♣7,♥K

 Code

def create():
    l = []
    for i in range(len(suit)):
        for k in range(len(d)):
            temp = suit[i]+d[k]
            l.append(temp)
    return l
def shufflecard(pokers):
    random.shuffle(pokers)
    return pokers
def deal(pokers,n):
    step = n-1 #开始的步长
    print('第{}个玩家拿到的牌是:'.format(n),end='')
    for i in range(5):
        if i !=4:
            print(pokers[step],end=',')
            step = step + 4
        else:
            print(pokers[step])
            step = step + 4

Submit results

 

Guess you like

Origin blog.csdn.net/weixin_58707437/article/details/127991891