我正在 Exercism 网站上进行一项练习,并且我已经编写了程序,但它没有通过 Exercism 对其进行的一项测试。不过,我不太清楚我需要做什么来修复它。这是我的代码:
import random
ABILITIES = ['strength', 'dexterity', 'constitution',
'intelligence', 'wisdom', 'charisma']
class Character:
def __init__(self):
for ability in ABILITIES:
setattr(self, ability, roll_ability())
self.hitpoints = 10 + modifier(self.constitution)
def modifier(constitution):
return (constitution - 10) // 2
def roll_ability(dice=4, sides=6):
rolls = []
for die in range(dice):
rolls.append(random.randint(1, sides))
rolls.remove(min(rolls))
return sum(rolls)
这是测试文件中失败的代码:
def test_random_ability_is_within_range(self):
score = Character().ability()
self.assertIs(score >= 3 and score <= 18, True)
这是失败消息:
________________________________ DndCharacterTest.test_random_ability_is_within_range _________________________________
self = <dnd_character_test.DndCharacterTest testMethod=test_random_ability_is_within_range>
def test_random_ability_is_within_range(self):
> score = Character().ability()
E AttributeError: 'Character' object has no attribute 'ability'
dnd_character_test.py:58: AttributeError
我想我需要一个名为“能力”的对象属性?但它有什么作用?我不喜欢必须如此专门地编写程序才能通过单元测试!我想我需要在开始编写代码之前通读单元测试,这样我才能知道该怎么做?
慕莱坞森
相关分类