元組的案例示範,元組案例示範

來源:互聯網
上載者:User

元組的案例示範,元組案例示範

案例一:輸入一個數字,轉換成中文數字。比如: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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.