<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # 14.3.?`roman.py`, 第 3 階段 現在 `toRoman` 對于有效的輸入 (`1` 到 `3999` 整數) 已能正確工作,是正確處理那些無效輸入 (任何其他輸入) 的時候了。 ## 例?14.6.?`roman3.py` 這個文件可以在例子目錄下的 `py/roman/stage3/` 目錄中找到。 如果您還沒有下載本書附帶的樣例程序, 可以 [下載本程序和其他樣例程序](http://www.woodpecker.org.cn/diveintopython/download/diveintopython-exampleszh-cn-5.4b.zip "Download example scripts")。 ``` """Convert to and from Roman numerals""" #Define exceptions class RomanError(Exception): pass class OutOfRangeError(RomanError): pass class NotIntegerError(RomanError): pass class InvalidRomanNumeralError(RomanError): pass #Define digit mapping romanNumeralMap = (('M', 1000), ('CM', 900), ('D', 500), ('CD', 400), ('C', 100), ('XC', 90), ('L', 50), ('XL', 40), ('X', 10), ('IX', 9), ('V', 5), ('IV', 4), ('I', 1)) def toRoman(n): """convert integer to Roman numeral""" if not (0 < n < 4000): raise OutOfRangeError, "number out of range (must be 1..3999)" if int(n) <> n: raise NotIntegerError, "non-integers can not be converted" result = "" for numeral, integer in romanNumeralMap: while n >= integer: result += numeral n -= integer return result def fromRoman(s): """convert Roman numeral to integer""" pass ``` | | | | --- | --- | | \[1\] | 這個寫法很 Pythonic:一次進行多個比較。這等價于`if not ((0 &lt; n) and (n &lt; 4000))`,但是更容易讓人理解。這是在進行范圍檢查,可以將過大的數、負數和零查出來。 | | \[2\] | 你使用 `raise` 語句引發自己的異常。你可以引發任何內建異常或者已定義的自定義異常。第二個參數是可選的,如果給定,則會在異常未被處理時顯示于追蹤信息 (trackback) 之中。 | | \[3\] | 這是一個非整數檢查。非整數無法轉化為羅馬數字表示。 | | \[4\] | 函數的其他部分未被更改。 | ## 例?14.7.?觀察 `toRoman` 如何處理無效輸入 ``` >>> import roman3 >>> roman3.toRoman(4000) Traceback (most recent call last): File "<interactive input>", line 1, in ? File "roman3.py", line 27, in toRoman raise OutOfRangeError, "number out of range (must be 1..3999)" OutOfRangeError: number out of range (must be 1..3999) >>> roman3.toRoman(1.5) Traceback (most recent call last): File "<interactive input>", line 1, in ? File "roman3.py", line 29, in toRoman raise NotIntegerError, "non-integers can not be converted" NotIntegerError: non-integers can not be converted ``` ## 例?14.8.?用 `romantest3.py` 測試 `roman3.py` 的結果 ``` fromRoman should only accept uppercase input ... FAIL toRoman should always return uppercase ... ok fromRoman should fail with malformed antecedents ... FAIL fromRoman should fail with repeated pairs of numerals ... FAIL fromRoman should fail with too many repeated numerals ... FAIL fromRoman should give known result with known input ... FAIL toRoman should give known result with known input ... ok fromRoman(toRoman(n))==n for all n ... FAIL toRoman should fail with non-integer input ... ok toRoman should fail with negative input ... ok toRoman should fail with large input ... ok toRoman should fail with 0 input ... ok ``` | | | | --- | --- | | \[1\] | `toRoman` 仍然能通過[已知值測試](testing_for_success.html#roman.testtoromanknownvalues.example "例?13.2.?testToRomanKnownValues"),這很令人鼓舞。所有[第 2 階段](stage_2.html "14.2.?roman.py, 第 2 階段")通過的測試仍然能通過,這說明新的代碼沒有對原有代碼構成任何負面影響。 | | \[2\] | 更令人振奮的是所有的[無效輸入測試](testing_for_failure.html#roman.tobadinput.example "例?13.3.?測試 toRoman 的無效輸入")現在都通過了。`testNonInteger` 這個測試能夠通過是因為有了 `int(n) &lt;&gt; n` 檢查。當一個非整數傳遞給 `toRoman` 時,`int(n) &lt;&gt; n` 檢查出問題并引發 `NotIntegerError` 異常,這正是 `testNonInteger` 所期待的。 | | \[3\] | `testNegative` 這個測試能夠通過是因為 `not (0 &lt; n &lt; 4000)` 檢查引發了 `testNegative` 期待的 `OutOfRangeError` 異常。 | ``` ====================================================================== FAIL: fromRoman should only accept uppercase input ---------------------------------------------------------------------- Traceback (most recent call last): File "C:\docbook\dip\py\roman\stage3\romantest3.py", line 156, in testFromRomanCase roman3.fromRoman, numeral.lower()) File "c:\python21\lib\unittest.py", line 266, in failUnlessRaises raise self.failureException, excName AssertionError: InvalidRomanNumeralError ====================================================================== FAIL: fromRoman should fail with malformed antecedents ---------------------------------------------------------------------- Traceback (most recent call last): File "C:\docbook\dip\py\roman\stage3\romantest3.py", line 133, in testMalformedAntecedent self.assertRaises(roman3.InvalidRomanNumeralError, roman3.fromRoman, s) File "c:\python21\lib\unittest.py", line 266, in failUnlessRaises raise self.failureException, excName AssertionError: InvalidRomanNumeralError ====================================================================== FAIL: fromRoman should fail with repeated pairs of numerals ---------------------------------------------------------------------- Traceback (most recent call last): File "C:\docbook\dip\py\roman\stage3\romantest3.py", line 127, in testRepeatedPairs self.assertRaises(roman3.InvalidRomanNumeralError, roman3.fromRoman, s) File "c:\python21\lib\unittest.py", line 266, in failUnlessRaises raise self.failureException, excName AssertionError: InvalidRomanNumeralError ====================================================================== FAIL: fromRoman should fail with too many repeated numerals ---------------------------------------------------------------------- Traceback (most recent call last): File "C:\docbook\dip\py\roman\stage3\romantest3.py", line 122, in testTooManyRepeatedNumerals self.assertRaises(roman3.InvalidRomanNumeralError, roman3.fromRoman, s) File "c:\python21\lib\unittest.py", line 266, in failUnlessRaises raise self.failureException, excName AssertionError: InvalidRomanNumeralError ====================================================================== FAIL: fromRoman should give known result with known input ---------------------------------------------------------------------- Traceback (most recent call last): File "C:\docbook\dip\py\roman\stage3\romantest3.py", line 99, in testFromRomanKnownValues self.assertEqual(integer, result) File "c:\python21\lib\unittest.py", line 273, in failUnlessEqual raise self.failureException, (msg or '%s != %s' % (first, second)) AssertionError: 1 != None ====================================================================== FAIL: fromRoman(toRoman(n))==n for all n ---------------------------------------------------------------------- Traceback (most recent call last): File "C:\docbook\dip\py\roman\stage3\romantest3.py", line 141, in testSanity self.assertEqual(integer, result) File "c:\python21\lib\unittest.py", line 273, in failUnlessEqual raise self.failureException, (msg or '%s != %s' % (first, second)) AssertionError: 1 != None ---------------------------------------------------------------------- Ran 12 tests in 0.401s FAILED (failures=6) ``` | | | | --- | --- | | \[1\] | 你已將失敗降至 6 個,而且它們都是關于 `fromRoman` 的:已知值測試、三個獨立的無效輸入測試,大小寫檢查和完備性檢查。這意味著 `toRoman` 通過了所有可以獨立通過的測試 (完備性測試也測試它,但需要 `fromRoman` 編寫后一起測試)。這就是說,你應該停止對 `toRoman` 的代碼編寫。不必再推敲,不必再做額外的檢查 “恰到好處”。停下來吧!現在,別再敲鍵盤了。 | > 注意 > 全面的單元測試能夠告訴你的最重要的事情是什么時候停止編寫代碼。當一個函數的所有單元測試都通過了,停止編寫這個函數。一旦整個模塊的單元測試通過了,停止編寫這個模塊。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看