第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何更改我的代碼以與此單元測(cè)試兼容?

如何更改我的代碼以與此單元測(cè)試兼容?

ibeautiful 2022-07-05 17:38:46
我正在 Exercism 網(wǎng)站上進(jìn)行一項(xiàng)練習(xí),并且我已經(jīng)編寫了程序,但它沒(méi)有通過(guò) Exercism 對(duì)其進(jìn)行的一項(xiàng)測(cè)試。不過(guò),我不太清楚我需要做什么來(lái)修復(fù)它。這是我的代碼:import randomABILITIES = ['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) // 2def 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)這是測(cè)試文件中失敗的代碼: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我想我需要一個(gè)名為“能力”的對(duì)象屬性?但它有什么作用?我不喜歡必須如此專門地編寫程序才能通過(guò)單元測(cè)試!我想我需要在開始編寫代碼之前通讀單元測(cè)試,這樣我才能知道該怎么做?
查看完整描述

1 回答

?
慕萊塢森

TA貢獻(xiàn)1810條經(jīng)驗(yàn) 獲得超4個(gè)贊

閱讀了D&D Character 練習(xí)描述后,這里根本沒(méi)有具體說(shuō)明。您對(duì)必須依靠這里的測(cè)試來(lái)為您提供規(guī)范感到不安是對(duì)的,這應(yīng)該在您的作業(yè)中更清楚地描述。


測(cè)試當(dāng)然期望有一個(gè)Character().ability()方法,并驗(yàn)證該方法返回一個(gè) 3-18 范圍內(nèi)的整數(shù),包括 3-18。因此,在描述告訴您如何計(jì)算能力以及測(cè)試正在尋找什么的內(nèi)容之間閱讀,您只需將您的roll_ability()函數(shù)移動(dòng)到您的Character類并重命名它ability():


class Character:

    def __init__(self):

        for ability in ABILITIES:

            setattr(self, ability, self.ability())

        self.hitpoints = 10 + modifier(self.constitution)


    def ability(self, dice=4, sides=6):

        rolls = []

        for die in range(dice):

            rolls.append(random.randint(1, sides))

        rolls.remove(min(rolls))

        return sum(rolls)

您自己的實(shí)現(xiàn)已經(jīng)產(chǎn)生了 3 到 18 之間的數(shù)字(包括 3 個(gè)最高骰子的總和),因此應(yīng)該毫無(wú)問(wèn)題地通過(guò)測(cè)試。我已經(jīng)確認(rèn)上述實(shí)現(xiàn)(加上你的modifier()函數(shù))確實(shí)通過(guò)了給定的單元測(cè)試。


從設(shè)計(jì)的角度來(lái)看,您在這里使用單獨(dú)的功能是正確的。ability()不依賴于任何Character狀態(tài),也不是字符實(shí)例預(yù)期執(zhí)行的功能。與其把它變成一個(gè)方法(帶有一個(gè)無(wú)用的self參數(shù)),你可以在這里妥協(xié)并把它變成一個(gè)@staticmethod:


class Character:

    def __init__(self):

        for ability in ABILITIES:

            setattr(self, ability, self.ability())

        self.hitpoints = 10 + modifier(self.constitution)


    @staticmethod

    def ability(dice=4, sides=6):

        rolls = []

        for die in range(dice):

            rolls.append(random.randint(1, sides))

        rolls.remove(min(rolls))

        return sum(rolls)

至于ability()函數(shù)實(shí)現(xiàn),您可能想在這里查看干凈有效地從 4 個(gè)骰子中選出前 3 個(gè)的heapq.nlargest()函數(shù):


from heapq import nlargest


class Character:

    # ...


    @staticmethod

    def ability(dice=4, sides=6):

        rolls = (random.randint(1, sides) for _ in range(dice))

        return sum(nlargest(dice - 1, rolls))

我只是根據(jù) YAGNIdice將andsides參數(shù)放在此處,或者至少將幻數(shù)移動(dòng)到頂部的大寫全局名稱中。46


查看完整回答
反對(duì) 回復(fù) 2022-07-05
  • 1 回答
  • 0 關(guān)注
  • 131 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)