输入a~d的字母对应扑克牌黑、红、梅、方花色 + 1~13数字对应扑克牌点数;输出“字母+数字”字符串对应的扑克牌花色和点数。
(本笔记适合熟悉Python循环和str字符串处理的coder翻阅)
-  Python 官网:https://www.python.org/ 
-  Free:大咖免费“圣经”教程《 python 完全自学教程》,不仅仅是基础那么简单…… 
 地址:https://lqpybook.readthedocs.io/
  自学并不是什么神秘的东西,一个人一辈子自学的时间总是比在学校学习的时间长,没有老师的时候总是比有老师的时候多。
             —— 华罗庚
- My CSDN主页、My HOT博、My Python 学习个人备忘录
- 好文力荐、 老齐教室
 
 
本文质量分:
CSDN质量分查询入口:http://www.csdn.net/qc
◆ 输出扑克牌
1、题目描述
- 题目描述截屏图片
 链图片
【题目来源于 CSDN 问答社区提问“title”】
2、算法解析
2.1 替换数据字典准备
Python代码
    cardDict = dict(zip('abcd', ['♠️', '♥️', '♣️', '♦️']))
    nums = [1, 11, 12, 13]
    numDict = dict(zip(nums, 'AJQK'))
2.2 if条件筛选实施字符替换
Python代码
    if findall(r"^[a-dA-D]\d[0-3]*$", s):
        a, n = s[0], s[1:]
        a = cardDict.get(s[0].lower()) # 置换花色。
        n = numDict.get(int(n)) if int(n) in nums else n # 置换点数。
        return f"{a}{n:>2}"
    else:
        print(f"\n{' 输入错误!':~^35}\n")
        exit()
showPoker函数
def showPoker(s):
    cardDict = dict(zip('abcd', ['♠️', '♥️', '♣️', '♦️']))
    nums = [1, 11, 12, 13]
    numDict = dict(zip(nums, 'AJQK'))
    if findall(r"^[a-dA-D]\d[0-3]*$", s):
        a, n = s[0], s[1:]
        a = cardDict.get(s[0].lower()) # 置换花色。
        n = numDict.get(int(n)) if int(n) in nums else n # 置换点数。
        return f"{a}{n:>2}"
    else:
        print(f"\n{' 输入错误!':~^35}\n")
        exit()
2.3 循环输出52张扑克牌
外层for遍历1-13的数字,内层for遍历abcd四种花色,可以调用showPoker按点数打印52张扑克牌;调换内外层for循环,可以调用showPoker函数按花色输出52扑克牌。
Python代码
def numShow():
    print('\n按点数输出52张扑克牌:')
    
    for i in range(1, 14):
        
        for j in 'abcd':
            print(showPoker(f"{j}{i}"), end=' '*4)
        
        print()
        
    print()
def colorShow():
    print('\n按花色输出52张扑克牌:')
    for i in 'abcd':        
        for j in range(1, 14):
            print(showPoker(f"{i}{j}"), end=' '*3)
        print('\n')
    print()
3、代码运行输出效果
- 截屏图片
  
 
 ▪- 文本
 a. 按点数输出52张扑克牌:
 ♠️ A ♥️ A ♣️ A ♦️ A
 ♠️ 2 ♥️ 2 ♣️ 2 ♦️ 2
 ♠️ 3 ♥️ 3 ♣️ 3 ♦️ 3
 ♠️ 4 ♥️ 4 ♣️ 4 ♦️ 4
 ♠️ 5 ♥️ 5 ♣️ 5 ♦️ 5
 ♠️ 6 ♥️ 6 ♣️ 6 ♦️ 6
 ♠️ 7 ♥️ 7 ♣️ 7 ♦️ 7
 ♠️ 8 ♥️ 8 ♣️ 8 ♦️ 8
 ♠️ 9 ♥️ 9 ♣️ 9 ♦️ 9
 ♠️10 ♥️10 ♣️10 ♦️10
 ♠️ J ♥️ J ♣️ J ♦️ J
 ♠️ Q ♥️ Q ♣️ Q ♦️ Q
 ♠️ K ♥️ K ♣️ K ♦️ K
 
 
 b. 按花色输出52张扑克牌:
 ♠️ A ♠️ 2 ♠️ 3 ♠️ 4 ♠️ 5 ♠️ 6 ♠️ 7 ♠️ 8 ♠️ 9 ♠️10 ♠️ J ♠️ Q ♠️ K
 
 ♥️ A ♥️ 2 ♥️ 3 ♥️ 4 ♥️ 5 ♥️ 6 ♥️ 7 ♥️ 8 ♥️ 9 ♥️10 ♥️ J ♥️ Q ♥️ K
 
 ♣️ A ♣️ 2 ♣️ 3 ♣️ 4 ♣️ 5 ♣️ 6 ♣️ 7 ♣️ 8 ♣️ 9 ♣️10 ♣️ J ♣️ Q ♣️ K
 
 ♦️ A ♦️ 2 ♦️ 3 ♦️ 4 ♦️ 5 ♦️ 6 ♦️ 7 ♦️ 8 ♦️ 9 ♦️10 ♦️ J ♦️ Q ♦️ K
4、完整源码
(源码较长,点此跳过源码)
#!/sur/bin/nve python
# coding: utf-8
from re import findall
def showPoker(s):
    cardDict = dict(zip('abcd', ['♠️', '♥️', '♣️', '♦️']))
    nums = [1, 11, 12, 13]
    numDict = dict(zip(nums, 'AJQK'))
    if findall(r"^[a-dA-D]\d[0-3]*$", s):
        a, n = s[0], s[1:]
        a = cardDict.get(s[0].lower()) # 置换花色。
        n = numDict.get(int(n)) if int(n) in nums else n # 置换点数。
        return f"{a}{n:>2}"
    else:
        print(f"\n{' 输入错误!':~^35}\n")
        exit()
def numShow():
    print('\n按点数输出52张扑克牌:')
    
    for i in range(1, 14):
        
        for j in 'abcd':
            print(showPoker(f"{j}{i}"), end=' '*4)
        
        print()
        
    print()
def colorShow():
    print('\n按花色输出52张扑克牌:')
    for i in 'abcd':        
        for j in range(1, 14):
            print(showPoker(f"{i}{j}"), end=' '*3)
        print('\n')
    print()
if __name__ == '__main__':
    #s = input('\n输入:').strip()
    #print(f'\n输出:{showPoker(s)}')
    numShow()
    colorShow()
上一篇: 经典循环命题:百钱百鸡(翁五钱一只,母三钱,小鸡三只一钱;百钱百鸡百鸡花百钱)
下一篇:
我的HOT博:
  本次共计收集 246 篇博文笔记信息,总阅读量 40.46w,平均阅读量 1644。已生成 16 篇阅读量不小于 4000 的博文笔记索引链接。数据采集于 2023-10-12 05:41:03 完成,用时 4 分 41.10 秒。
- ChatGPT国内镜像站初体验:聊天、Python代码生成等
 ( 59262 阅读)
 博文地址:https://blog.csdn.net/m0_57158496/article/details/129035387
 点赞:126 踩 :0 收藏:798 打赏:0 评论:71
 本篇博文笔记于 2023-02-14 23:46:33 首发,最晚于 2023-07-03 05:50:55 修改。
- 让QQ群昵称色变的神奇代码
 ( 58086 阅读)
 博文地址:https://blog.csdn.net/m0_57158496/article/details/122566500
 点赞:24 踩 :0 收藏:83 打赏:0 评论:17
 本篇博文笔记于 2022-01-18 19:15:08 首发,最晚于 2022-01-20 07:56:47 修改。
- pandas 数据类型之 DataFrame
 ( 9173 阅读)
 博文地址:https://blog.csdn.net/m0_57158496/article/details/124525814
 点赞:6 踩 :0 收藏:31 打赏:0 评论:0
 本篇博文笔记于 2022-05-01 13:20:17 首发,最晚于 2022-05-08 08:46:13 修改。
- 个人信息提取(字符串)
 ( 7215 阅读)
 博文地址:https://blog.csdn.net/m0_57158496/article/details/124244618
 点赞:1 踩 :0 收藏:13 打赏:0 评论:0
 本篇博文笔记于 2022-04-18 11:07:12 首发,最晚于 2022-04-20 13:17:54 修改。
- Python列表(list)反序(降序)的7种实现方式
 ( 7161 阅读)
 博文地址:https://blog.csdn.net/m0_57158496/article/details/128271700
 点赞:5 踩 :0 收藏:22 打赏:0 评论:8
 本篇博文笔记于 2022-12-11 23:54:15 首发,最晚于 2023-03-20 18:13:55 修改。
- 罗马数字转换器|罗马数字生成器
 ( 7035 阅读)
 博文地址:https://blog.csdn.net/m0_57158496/article/details/122592047
 点赞:0 踩 :0 收藏:1 打赏:0 评论:0
 本篇博文笔记于 2022-01-19 23:26:42 首发,最晚于 2022-01-21 18:37:46 修改。
- Python字符串居中显示
 ( 6966 阅读)
 博文地址:https://blog.csdn.net/m0_57158496/article/details/122163023
 点赞:1 踩 :0 收藏:7 打赏:0 评论:1
 本篇博文笔记
- 斐波那契数列的递归实现和for实现
 ( 5523 阅读)
 博文地址:https://blog.csdn.net/m0_57158496/article/details/122355295
 点赞:4 踩 :0 收藏:2 打赏:0 评论:8
 本篇博文笔记
- python清屏
 ( 5108 阅读)
 博文地址:https://blog.csdn.net/m0_57158496/article/details/120762101
 点赞:0 踩 :0 收藏:8 打赏:0 评论:0
 本篇博文笔记
- 练习:字符串统计(坑:f‘string‘报错)
 ( 5103 阅读)
 博文地址:https://blog.csdn.net/m0_57158496/article/details/121723096
 点赞:0 踩 :0 收藏:1 打赏:0 评论:0
 本篇博文笔记
- 回车符、换行符和回车换行符
 ( 5093 阅读)
 博文地址:https://blog.csdn.net/m0_57158496/article/details/123109488
 点赞:1 踩 :0 收藏:2 打赏:0 评论:0
 本篇博文笔记于 2022-02-24 13:10:02 首发,最晚于 2022-02-25 20:07:40 修改。
- 练习:尼姆游戏(聪明版/傻瓜式•人机对战)
 ( 4943 阅读)
 博文地址:https://blog.csdn.net/m0_57158496/article/details/121645399
 点赞:14 踩 :0 收藏:42 打赏:0 评论:0
 本篇博文笔记
- 密码强度检测器
 ( 4323 阅读)
 博文地址:https://blog.csdn.net/m0_57158496/article/details/121739694
 点赞:1 踩 :0 收藏:4 打赏:0 评论:0
 本篇博文笔记于 2021-12-06 09:08:25 首发,最晚于 2022-11-27 09:39:39 修改。
- 练习:生成100个随机正整数
 ( 4274 阅读)
 博文地址:https://blog.csdn.net/m0_57158496/article/details/122558220
 点赞:1 踩 :0 收藏:6 打赏:0 评论:0
 本篇博文笔记于 2022-01-18 13:31:36 首发,最晚于 2022-01-20 07:58:12 修改。
- 我的 Python.color() (Python 色彩打印控制)
 ( 4159 阅读)
 博文地址:https://blog.csdn.net/m0_57158496/article/details/123194259
 点赞:2 踩 :0 收藏:8 打赏:0 评论:0
 本篇博文笔记于 2022-02-28 22:46:21 首发,最晚于 2022-03-03 10:30:03 修改。
- 罗马数字转换器(用罗马数字构造元素的值取模实现)
 ( 4149 阅读)
 博文地址:https://blog.csdn.net/m0_57158496/article/details/122608526
 点赞:0 踩 :0 收藏:0 打赏:0 评论:0
 本篇博文笔记于 2022-01-20 19:38:12 首发,最晚于 2022-01-21 18:32:02 修改。
 
 
精品文章:
- 好文力荐:齐伟书稿 《python 完全自学教程》 Free连载(已完稿并集结成书,还有PDF版本百度网盘永久分享,点击跳转免费🆓下载。)
- OPP三大特性:封装中的property
- 通过内置对象理解python'
- 正则表达式
- python中“*”的作用
- Python 完全自学手册
- 海象运算符
- Python中的 `!=`与`is not`不同
- 学习编程的正确方法
◆ Python 入门指南【Python 3.6.3】
好文力荐:
- 全栈领域优质创作者——[寒佬](还是国内某高校学生)博文“非技术文—关于英语和如何正确的提问”,“英语”和“会提问”是编程学习的两大利器。
- 【8大编程语言的适用领域】先别着急选语言学编程,先看它们能干嘛
- 靠谱程序员的好习惯
- 大佬帅地的优质好文“函数功能、结束条件、函数等价式”三大要素让您认清递归
CSDN实用技巧博文:
- 8个好用到爆的Python实用技巧
- python忽略警告
- Python代码编写规范
- Python的docstring规范(说明文档的规范写法)





