Zanurkuj w Pythonie/roman.py, etap 3: Różnice pomiędzy wersjami

Usunięta treść Dodana treść
Piotr (dyskusja | edycje)
mNie podano opisu zmian
Piotr (dyskusja | edycje)
Nie podano opisu zmian
Linia 2:
 
{{Podświetl|py}}
== <tt>roman.py</tt>, stageetap 3 ==
 
Teraz już <tt>toRoman</tt> odpowiednio sobie radzi z dobrym wejściem (liczbami całkowitymi od 1 do 3999), więc teraz jest czas zając się niepoprawnym wejściem (wszystkim innym).
Now that toRoman behaves correctly with good input (integers from 1 to 3999), it's time to make it behave correctly with bad input (everything else).
 
'''ExamplePrzykład 14.6. <tt>roman3.py</tt>'''
 
ThisPlik fileten isjest availabledostępny inz <tt>py/roman/stage3/</tt> in thew exampleskatalogu directoryprzykładów.
 
Jeśli jeszcze tego nie zrobiłeś, możesz pobrać [http://diveintopython.org/download/diveintopython-examples-5.4.zip ten i inne przykłady wykorzystane w tej książce].
If you have not already done so, you can download this and other examples used in this book.
 
"""Convert to and from Roman numerals"""
Linia 53:
pass
# ThisJest isto aprzyjemny nicepythonowy Pythonic shortcutskrót: multiple comparisons atwielokrotne onceporównanie. ThisJest isto equivalentodpowiedniek todo <tt>if not ((0 < n) and (n < 4000))</tt>, butjednak it'słatwiejszy muchdo easierodczytu. toZa read.pomocą Thistego iskontrolujemy thezakres rangewartości checki sprawdzamy, andczy itwprowadzona shouldliczba catchnie inputsjest thatza are too largeduża, negativeujemna, orczy też równa zero.
# Wyrzucamy wyjątek za pomocą wyrażenia <tt>raise</tt>. Możemy wyrzucić każdy wbudowane wyjątek, a także inny zdefiniowany przez nas wyjątek. Drugi parametr, ''wiadomość błędu'', jest opcjonalny; jeśli dostaniemy wyjątek i nigdzie jego nie obsłużymy, zostanie on wyświetlone w traceback (w postaci śladów stosu).
# You raise exceptions yourself with the raise statement. You can raise any of the built-in exceptions, or you can raise any of your custom exceptions that you've defined. The second parameter, the error message, is optional; if given, it is displayed in the traceback that is printed if the exception is never handled.
# Za pomocą tego sprawdzamy, czy liczby nie jest całkowita. Liczby nie będące liczbami całkowitymi nie mogą zostać przekonwertowane na system rzymski.
# This is the non-integer check. Non-integers can not be converted to Roman numerals.
# Pozostała część funkcji jest niezmieniona.
# The rest of the function is unchanged.
 
'''ExamplePrzykład 14.7. WatchingObserwujemy, jak <tt>toRoman</tt> radzi sobie handlez badbłędnym inputwejściem'''
 
>>> import roman3
Linia 74:
NotIntegerError: non-integers can not be converted
'''ExamplePrzykład 14.8. Output ofWyjście <tt>romantest3.py</tt> w zależności againstod <tt>roman3.py</tt>'''
 
fromRoman should only accept uppercase input ... FAIL