|
|
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))
|
|