LoginSignup
1
0

More than 3 years have passed since last update.

Pythonの技術力を向上させるテスト、2題。コード量は1行と2行。(エラーメッセージ編)

Last updated at Posted at 2021-02-13

はじめに

とにかく、「ルール」を確認して、解いたり、(人に)解かしてみたりして下さい。技術力の確認、向上につながるかと。

この問題に両方正解できた方は、初心者のレベルは絶対に超えている :tada: と思います。
かりに、中級者の技術力があることの確認とさせて下さい。

2題は、各々、1行と2行のコードからなります。
テーマは、エラーメッセージです。(回答は、末尾です。

ルール

  • 解は、2つ程度まで案出あり。
  • Pythonを実行させて←:no_good:そのエラーの様子をみてから考えるのは、NG。頭だけで考えて下さい。
  • ただし、ネット等で調べるのは、問題なし。
  • Python3.7(windows)を前提にしていますが、、、特に、細かいバージョンには影響されないと思います。
  • 制限時間は、ありません。

おことわり

のりとしては、クイズなので、Pythonの手練れの方でも、クイズが苦手な方は、正解できないと思います。それは、Pythonの技術力じゃなくて、クイズとかの得手不得手かと。

エラーメッセージ編[第1問]

出題
以下のコードにおける「text_file_ha_nai.txt」は、存在しないファイルです。
よって、エラーがでますが、エラーは、FileNotFoundErrorです。
下記コードに1文字加えてOSErrorが出るようにして下さい。
f = open('text_file_ha_nai.txt')

<補足>
1文字加えるの意味がわかりにくいと思いますが、
例1)f += open('text_file_ha_nai.txt') ## +を加えている
例2) f = _open('text_file_ha_nai.txt') ## _を加えている

エラーメッセージ編[第2問]

出題
以下のコードは、普通に実行されて'100'と表示されると思いますが、下記コードの1行目に連続する2、3文字加えてIndexErrorが出るようにして下さい。
注意:exclamation: : 連続する文字です。1行目です。
a = [100,200,300]
print(a[0])

<補足>
2、3文字加え加えるの意味がわかりにくいと思いますが、
例1)a = [100,200,300,,,] ## ,,,の3文字を加えている

参考

以下のQiitaの記事に、例外ツリーの確認方法が示されていたので、実行してみた。

実行結果は、以下。


Python Version: 3.7.2

BaseException
... Exception
...... ArithmeticError
......... FloatingPointError
......... OverflowError
......... ZeroDivisionError
...... AssertionError
...... AttributeError
...... BufferError
...... EOFError
...... Error (<class 'locale.Error'>)
...... ImportError
......... ModuleNotFoundError
......... ZipImportError
...... LookupError
......... CodecRegistryError
......... IndexError
......... KeyError
...... MemoryError
...... NameError
......... UnboundLocalError
...... OSError
......... BlockingIOError
......... ChildProcessError
......... ConnectionError
............ BrokenPipeError
............ ConnectionAbortedError
............ ConnectionRefusedError
............ ConnectionResetError
......... FileExistsError
......... FileNotFoundError
......... InterruptedError
......... IsADirectoryError
......... NotADirectoryError
......... PermissionError
......... ProcessLookupError
......... TimeoutError
......... UnsupportedOperation
...... ReferenceError
...... RuntimeError
......... BrokenBarrierError
......... NotImplementedError
......... RecursionError
......... _DeadlockError
...... StopAsyncIteration
...... StopIteration
...... StopTokenizing
...... SubprocessError
......... CalledProcessError
......... TimeoutExpired
...... SyntaxError
......... IndentationError
............ TabError
...... SystemError
......... CodecRegistryError
...... TokenError
...... TypeError
...... ValueError
......... UnicodeError
............ UnicodeDecodeError
............ UnicodeEncodeError
............ UnicodeTranslateError
......... UnsupportedOperation
...... Verbose
...... Warning
......... BytesWarning
......... DeprecationWarning
......... FutureWarning
......... ImportWarning
......... PendingDeprecationWarning
......... ResourceWarning
......... RuntimeWarning
......... SyntaxWarning
......... UnicodeWarning
......... UserWarning
...... _OptionError
...... error (<class 're.error'>)
... GeneratorExit
... KeyboardInterrupt
... SystemExit


まとめ の前に

2問とも、そこそこ、難問だと思います。
また、ちょっと、クイズ的要素が高い気もしますが、
何の役にも立たない情報ということもないと思います。

まとめ

特に、ありません。
ワタシ自身は、初心者(しかし、2年近くはPythonはながめているので、超初心者では全然ないです。コードをバリバリ書くとかはしないので、時間がたっても初心者。。。)
どちらかというとこれは、クイズ的な要素が高い気がします、すみません。
実際にエラーが出たときの対処に貢献できれば、幸甚です。
(記事にしてて、あまり、面白くないと感じつつ。。。。)

お薦め書籍

以下のPython Pocket Reference (O'REILLY)が、すごく、使いやすい!

 Python Pocket Reference, 5th Edition
 Python In Your Pocket
 By Mark Lutz
 Publisher: O'Reilly Media

( 出典: https://www.oreilly.com/library/view/python-pocket-reference/9781449357009/

image.png

正解

第一問

f = open('\text_file_ha_nai.txt')
>python mondai_kai_1.py
Traceback (most recent call last):
  File "mondai_kai_1.py", line 1, in <module>
    f = open('\text_file_ha_nai.txt')
OSError: [Errno 22] Invalid argument: '\text_file_ha_nai.txt'

第二問

a = [100,200,300]*0
print(a[0])
>python mondai_kai_2.py
Traceback (most recent call last):
  File "mondai_kai_2.py", line 2, in <module>
    print(a[0])
IndexError: list index out of range
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0