本关任务:编写一个将牌排序的小程序。
第1关 按顺序输出新牌
def start():
"""初始顺序,返回元素为字符串的列表"""
########## Begin ##########
list = []
for i in desigh:
for n in num:
list.append(i+n)
return list + ghost
########## End ##########
if __name__ == '__main__':
desigh = ['♠', '♥', '♣', '♦'] # 表示黑桃、红桃、梅花、方块
num = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']
ghost = ['jokers', 'JOKERS']
cards = start()
print('新牌顺序')
print(*cards)
第2关 按洗好的顺序输出洗过的牌
import random
def shuffle_cards(cards_start):
"""打乱顺序,返回元素为字符串的列表"""
########## Begin ##########
random.shuffle(cards_start)
return cards_start
########## End ##########
def start():
"""初始顺序,返回元素为字符串的列表"""
cards_start = [i + j for i in desigh for j in num] + ghost
return cards_start
if __name__ == '__main__':
desigh = ['♠', '♥', '♣', '♦'] # 表示黑桃、红桃、梅花、方块
num = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']
ghost = ['jokers', 'JOKERS']
s = int(input())
random.seed(s)
cards = start()
cards_after = shuffle_cards(cards)
print('洗牌顺序')
print(*cards_after)
第3关 按分牌的顺序输出每个人手上的牌
import random
def traver(cards_shuffle, m):
"""发牌给m个人,返回二维列表"""
########## Begin ##########
palyerlist = []
for i in range(m):
palyerlist.append(cards_after[i::m])
return palyerlist
########## End ##########
def start():
"""初始顺序,返回元素为字符串的列表"""
cards_start = [i + j for i in desigh for j in num] + ghost
return cards_start
def shuffle_cards(cards_start):
"""打乱顺序,返回元素为字符串的列表"""
random.shuffle(cards_start)
return cards_start
if __name__ == '__main__':
desigh = ['♠', '♥', '♣', '♦'] # 表示黑桃、红桃、梅花、方块
num = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']
ghost = ['jokers', 'JOKERS']
n = int(input()) # 输入参与游戏的人数
s = int(input())
random.seed(s)
print(f'参与游戏的人数:{n}')
cards = start()
cards_after = shuffle_cards(cards)
cards_n = traver(cards_after, n)
print('每个人手上分到的牌')
for i in range(n):
print(*cards_n[i])
第4关 输出每个人手上升序排序的牌
import random
def sort_cards(person, m):
"""对m个人手上的牌进行升序排序,花色按黑红梅方,牌面按点数,大王最大,小王第二大"""
########## Begin ##########
desighs = {'♠': 1, '♥': 2, '♣': 3, '♦': 4, 'j': 5, 'J': 6} # 表示黑桃、红桃、梅花、方块
nums = {'2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, '10': 10, 'J': 11, 'Q': 12, 'K': 13,
'A': 14, 'okers': 15, 'OKERS': 16}
list = []
for i in person:
result_list = sorted(i, key=lambda x: (desighs[x[0]], nums[x[1:]]))
list.append(result_list)
return list
########## End ##########
def start():
"""初始顺序,返回元素为字符串的列表"""
cards_start = [i + j for i in desigh for j in num] + ghost
return cards_start
def shuffle_cards(cards_start):
"""打乱顺序,返回元素为字符串的列表"""
random.shuffle(cards_start)
return cards_start
def traver(cards_shuffle, m):
"""发牌给m个人,返回二维列表"""
person = []
for i in range(m):
person.append(cards_shuffle[i::m])
return person
if __name__ == '__main__':
desigh = ['♠', '♥', '♣', '♦'] # 表示黑桃、红桃、梅花、方块
num = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']
ghost = ['jokers', 'JOKERS']
n = int(input()) # 输入参与游戏的人数
s = int(input())
random.seed(s)
print(f'参与游戏的人数:{n}')
cards = start()
cards_after = shuffle_cards(cards)
cards_n = traver(cards_after, n)
cards_sort = sort_cards(cards_n, n)
print('每个人手上排序的牌')
for i in range(n):
print(*cards_sort[i])
喜乐有分享共度日月长,我等的人,已经来了❤