码迷,mamicode.com
首页 > 其他好文 > 详细

元组的案例演示

时间:2018-04-27 16:43:24      阅读:130      评论:0      收藏:0      [点我收藏+]

标签:元组的案例

案例01输入一个数字,转换成中文数字。比如:1234567890 -> 壹贰叁肆伍陆柒捌玖零

方法01

number = input("请输入一个数字:")

for i in range(len(number)):

    if "0" in number[i]:

        print("", end="")

    if "1" in number[i]:

        print("", end="")

    if "2" in number[i]:

        print("", end="")

    if "3" in number[i]:

        print("", end="")

    if "4" in number[i]:

        print("", end="")

    if "5" in number[i]:

        print("", end="")

    if "6" in number[i]:

        print("", end="")

    if "7" in number[i]:

        print("", end="")

    if "8" in number[i]:

        print("", end="")

    if "9" in number[i]:

        print("", end="")

    if "." in number[i]:

        print("", end="")

 

方法02:使用元组

chinese_number = ("", "", "", "", "", "", "", "", "", "")

number = input("请输入一个数字:")

for i in range(len(number)):

    if "." in number[i]:

        print("", end="")

    else:

        print(chinese_number[int(number[i])], end="")

 

执行结果:

C:\python\python.exe C:/python/demo/file3.py

请输入一个数字:123.45

壹贰叁点肆伍

Process finished with exit code 0

 

案例02根据花色和数字生成一副扑克牌

poker_type = ("?", "?", "?", "?")

poker_number = ("3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2")

poker_list = []

for i in poker_number:

    for j in poker_type:

        poker_list.append(j+i)

print(len(poker_list))

print(poker_list)

 

执行结果:

C:\python\python.exe C:/python/demo/file3.py

52

['?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', '?A', '?A', '?A', '?A', '?2', '?2', '?2', '?2']

 

Process finished with exit code 0


元组的案例演示

标签:元组的案例

原文地址:http://blog.51cto.com/13043937/2108560

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!