亞東科技大學 電機系 施勢帆老師的教學網站

 找回密碼
 立即註冊
搜索
熱搜: 活動 交友 discuz
查看: 10|回復: 0

RPG

[複製鏈接]

66

主題

66

帖子

314

積分

管理員

Rank: 9Rank: 9Rank: 9

積分
314
發表於 前天 16:13 | 顯示全部樓層 |閱讀模式
full_dot = '●'
empty_dot = '○'


def create_character(name, strength, intelligence, charisma):

    # 1. 驗證角色名稱是否為字串
    if not isinstance(name, str):
        return "The character name should be a string"

    # 2. 驗證角色名稱是否為空字串
    if name == "":
        return "The character should have a name"

    # 3. 驗證角色名稱長度是否超過 10 個字元
    if len(name) > 10:
        return "The character name is too long"

    # 4. 驗證角色名稱是否包含空白
    if " " in name:
        return "The character name should not contain spaces"

    # 5. 驗證三個屬性是否都是整數
    stats = [strength, intelligence, charisma]

    if not all(isinstance(stat, int) and not isinstance(stat, bool)
               for stat in stats):
        return "All stats should be integers"

    # 6. 驗證屬性是否小於 1
    if any(stat < 1 for stat in stats):
        return "All stats should be no less than 1"

    # 7. 驗證屬性是否大於 4
    if any(stat > 4 for stat in stats):
        return "All stats should be no more than 4"

    # 8. 驗證屬性總和是否等於 7
    if sum(stats) != 7:
        return "The character should start with 7 points"

    # 9. 建立角色屬性顯示函式
    def create_stat_line(label, value):
        filled = "●" * value
        empty = "○" * (10 - value)
        return label + " " + filled + empty

    # 10. 組合角色資料
    character = (
        name + "\n" +
        create_stat_line("STR", strength) + "\n" +
        create_stat_line("INT", intelligence) + "\n" +
        create_stat_line("CHA", charisma)
    )

    return character


# 測試程式
print(create_character("ren", 4, 2, 1))



回復

使用道具 舉報

您需要登錄後才可以回帖 登錄 | 立即註冊

本版積分規則

Archiver|手機版|小黑屋|亞東科技大學 電機系 施勢帆老師的教學網站

GMT+8, 2026-11-5 00:40 , Processed in 0.033675 second(s), 20 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回復 返回頂部 返回列表