{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "view-in-github"
},
"source": [
"
"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "4_5p-ivElTgJ"
},
"source": [
"# よくあるエラー集"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "JvTtKsM5pmDk"
},
"source": [
"このノートブックにはPythonを触っていてよく起こりがちなエラーの例をまとめます. \n",
"**皆さんの手元で起こったエラーの例も共有したいので、ぜひ教えてください**"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "eYKyKUoYmDRE"
},
"source": [
"\n",
"エラーには、いろいろな分類の仕方がありますが代表的なものは以下のとおりです:\n",
"\n",
"* 構文エラー (syntax error)\n",
"* ランタイムエラー (runtime error) または 例外 (exception)\n",
"* 論理エラー (logic error)\n",
"\n",
"これらの代表的な例を以下で紹介します。\n",
"\n",
"pythonのようなインタプリタ言語では、実行時にコードの有効性が検証されるため、構文エラーはランタイムエラーとも言えますが、エラーメッセージに従って分類することにします。\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "SpDDGwnLnKqK"
},
"source": [
"## 構文エラー\n",
"\n",
"構文エラーとは、プログラミング言語の文法・構文規則に違反していることによるエラーです。 \n",
"代表的なものは\n",
"\n",
"* 括弧や引用符の閉じ忘れ\n",
"* コロンのつけ忘れ\n",
"* ピリオドとカンマの間違い\n",
"* 全角記号の使用\n",
"* ```=```(代入)と```==```(等号)の混同\n",
"* 不適切なインデント\n",
"\n",
"などがあります。"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "aZyMaLbLoQK-"
},
"source": [
"**引用符のつけ忘れ**"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 134
},
"id": "t_Q-dc0IoO0r",
"outputId": "5c62d651-f031-4ea7-b479-6304fc6a9bbb"
},
"outputs": [
{
"ename": "SyntaxError",
"evalue": "ignored",
"output_type": "error",
"traceback": [
"\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m print(\"Hello World)\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m EOL while scanning string literal\n"
]
}
],
"source": [
"print(\"Hello World) "
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "ELW2rnF0oUd3"
},
"source": [
"**括弧の閉じ忘れ**"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 134
},
"id": "1-pTnHTEoYsM",
"outputId": "64e6d562-5e01-418c-dfe7-297f24096eba"
},
"outputs": [
{
"ename": "SyntaxError",
"evalue": "ignored",
"output_type": "error",
"traceback": [
"\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m print(\"Hello World\"\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m unexpected EOF while parsing\n"
]
}
],
"source": [
"print(\"Hello World\" "
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "zpAXxTJvofVN"
},
"source": [
"**ピリオドとカンマの間違い**"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 134
},
"id": "ZkxkEWp4ojjV",
"outputId": "827a5e7d-bf65-4250-db41-fb2fae93630a"
},
"outputs": [
{
"ename": "SyntaxError",
"evalue": "ignored",
"output_type": "error",
"traceback": [
"\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m a = [2.2. 3.14] # ほんとはa = [2.2, 3.14]のつもり\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n"
]
}
],
"source": [
"a = [2.2. 3.14] # ほんとはa = [2.2, 3.14]のつもり"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Uyfg5Kwgn4QP"
},
"source": [
"**コロンのつけ忘れ**"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 134
},
"id": "6Jq2pSYknQV3",
"outputId": "6e4d5ced-5ed2-492f-a507-ac52266aceb5"
},
"outputs": [
{
"ename": "SyntaxError",
"evalue": "ignored",
"output_type": "error",
"traceback": [
"\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m for i in range(2)\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n"
]
}
],
"source": [
"for i in range(2)\n",
" print(i)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "iyYf_NVRpH7x"
},
"source": [
"**全角記号の使用**\n",
"\n",
"これは見た目では気が付きづらいので、**\"invalid character\"というSyntaxErrorが出たときは、全角を疑う**というのもおすすめです。"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 134
},
"id": "SX39eEOco8iW",
"outputId": "893459db-9bd1-4d27-cf28-14b5c56d1d77"
},
"outputs": [
{
"ename": "SyntaxError",
"evalue": "ignored",
"output_type": "error",
"traceback": [
"\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m 1+1 #後ろの1が全角になっている\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid character in identifier\n"
]
}
],
"source": [
"1+1 #後ろの1が全角になっている"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 134
},
"id": "smCVbWbqpDhp",
"outputId": "e2620838-ea98-457d-fde5-37c09f030bf1"
},
"outputs": [
{
"ename": "SyntaxError",
"evalue": "ignored",
"output_type": "error",
"traceback": [
"\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m 1+2\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid character in identifier\n"
]
}
],
"source": [
"1+2 # 和記号+が全角になっている"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"id": "YMM02tBIpMRS",
"outputId": "43e549e4-f66e-4e8a-b1c3-b997dfe6ad96"
},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 16,
"metadata": {
"tags": []
},
"output_type": "execute_result"
}
],
"source": [
"1 + 1 #これはOK"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 134
},
"id": "gQYVZ3HYpPUM",
"outputId": "2858a768-f80f-4ff6-ff78-b5629b107eb4"
},
"outputs": [
{
"ename": "SyntaxError",
"evalue": "ignored",
"output_type": "error",
"traceback": [
"\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m 1 + 1\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid character in identifier\n"
]
}
],
"source": [
"1 + 1 #+と後ろの1の間に全角スペースが入っている"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "P5ijbBANp16z"
},
"source": [
"**=(代入)と==(等号)の混同**"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 134
},
"id": "x8onYf_MqLVL",
"outputId": "698727a2-36a8-4c26-ae45-a4f39f38c416"
},
"outputs": [
{
"ename": "SyntaxError",
"evalue": "ignored",
"output_type": "error",
"traceback": [
"\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m3\u001b[0m\n\u001b[0;31m if a = b : #ほんとは==のつもり\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n"
]
}
],
"source": [
"a = 2.0\n",
"b = 3.0\n",
"if a = b : #ほんとは==のつもり\n",
" print(\"a equal b!!\")"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "7Jj0xhgIqjlI"
},
"source": [
"**不適切なインデント**"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "GoxOOulCv2Z1"
},
"source": [
"下のコードは[予期しないインデント]というエラーがでる。"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 134
},
"id": "QIK7JWJqqlLO",
"outputId": "17b625d7-6a49-4dc0-9458-c463614aec97"
},
"outputs": [
{
"ename": "IndentationError",
"evalue": "ignored",
"output_type": "error",
"traceback": [
"\u001b[0;36m File \u001b[0;32m\"\"\u001b[0;36m, line \u001b[0;32m3\u001b[0m\n\u001b[0;31m for j in range(5):\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mIndentationError\u001b[0m\u001b[0;31m:\u001b[0m unexpected indent\n"
]
}
],
"source": [
"for i in range(5):\n",
" print(i) \n",
" for j in range(5): ## この行と次の行のブロックが不要にインデントされている\n",
" print(j)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "ncV6G7KvrHKW"
},
"source": [
"## ランタイムエラー\n",
"\n",
"ランタイムエラーとは、 \n",
"書き方そのものはPythonの文法的に誤りではないものの、 \n",
"実際にコードを実行するとエラーになってしまうものを指します。\n",
"\n",
"非常に多岐に渡りますが、代表的なものとしては、\n",
"\n",
"* 範囲外参照\n",
"* 定義されていない変数の参照\n",
"* 意図しない変数の型に対する演算\n",
"* 0除算\n",
"\n",
"などがあります。"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "7qBpnEkqrwNV"
},
"source": [
"**範囲外参照**\n",
"\n",
"要素が2つしかないリストの3番目の要素にアクセスしようとすると、\n",
"当然エラーが出ます。"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 185
},
"id": "qyUnlHCzr_K_",
"outputId": "7c9384e5-d442-4c52-ed4c-e2882106288d"
},
"outputs": [
{
"ename": "IndexError",
"evalue": "ignored",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mIndexError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0ma\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m \u001b[0;34m\"いちご\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"りんご\"\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0ma\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mIndexError\u001b[0m: list index out of range"
]
}
],
"source": [
"a = [ \"いちご\", \"りんご\"]\n",
"a[2]"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "kb2WM6BLtbYK"
},
"source": [
"関数の定義域を超えて引数に入れるのもダメです"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 185
},
"id": "gWGn5zoithdY",
"outputId": "8ceae3e1-f923-4a0a-9a25-3285fb7e2dcf"
},
"outputs": [
{
"ename": "ValueError",
"evalue": "ignored",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mmath\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mmath\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlog\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m0.0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mValueError\u001b[0m: math domain error"
]
}
],
"source": [
"import math \n",
"math.log(0.0)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "IJFs-2jnsDeK"
},
"source": [
"**定義されていない変数の参照**"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "uNO8rFMYsHiI"
},
"source": [
"当然、定義されていない変数を使うとエラーが起こります"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 168
},
"id": "V1H-oFVFsF9x",
"outputId": "ce8d0ade-07f4-4223-bc52-99198bb2dbc4"
},
"outputs": [
{
"ename": "NameError",
"evalue": "ignored",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnum\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mNameError\u001b[0m: name 'num' is not defined"
]
}
],
"source": [
"print(num)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**ノートブック特有の注意点**\n",
"\n",
"作業途中に定義していた変数がメモリに残っていて、そのおかげで動いているが、\n",
"後日作業を再開した際(新しいセッションで実行したとき)にその変数が未定義となり動かなくなる。\n",
"といったことも稀に起こります。\n",
"とくに、セルを跨いで実行したりするノートブックの編集・実行においては、よく起こるミスです。\n",
"こうしたエラーを避けるには、適宜ランタイムやカーネルなどを再起動して、\n",
"新しいセッションで実行できるか確認するなどの対策が必要です。"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Y5x3eqQrsMQB"
},
"source": [
"打ち間違いにも注意です"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 185
},
"id": "8NAoPZHZsNbh",
"outputId": "64d1047d-66fa-423f-e912-21d9f900997d"
},
"outputs": [
{
"ename": "NameError",
"evalue": "ignored",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0minput\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m\"sample_file.txt\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m \u001b[0mimput\u001b[0m \u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mNameError\u001b[0m: name 'imput' is not defined"
]
}
],
"source": [
"input = \"sample_file.txt\"\n",
"print( imput )"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "q-he-J69sS-Z"
},
"source": [
"**意図しない演算**\n",
"\n",
"数値と数値の足し算は出来ますが、\n",
"数値と文字のように型が異なる変数を足すことはできません。"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 168
},
"id": "Iovexbyrse_B",
"outputId": "6ffb6719-b2db-4c87-f332-0d78bab79646"
},
"outputs": [
{
"ename": "TypeError",
"evalue": "ignored",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;34m\"私は\"\u001b[0m\u001b[0;34m+\u001b[0m \u001b[0;36m200\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0;34m\"歳です\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m: must be str, not int"
]
}
],
"source": [
"\"私は\"+ 200 + \"歳です\""
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "zCWRYfAnJW8h"
},
"source": [
"和に限らず\"演算\"は一般に特定の型(同士)のみに定義されています。"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "GqJC1Plfk-0R"
},
"source": [
"また、リスト等の要素にアクセスするのに、 \n",
"角括弧と間違えて丸括弧を使うミスもよく見られます。"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 202
},
"id": "HHOWbWlBk_LK",
"outputId": "4a1d5377-ce2f-4449-a14a-0fdc608d1026"
},
"outputs": [
{
"ename": "TypeError",
"evalue": "ignored",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0ma\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m3\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0ma\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;31m#これはOK\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0ma\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m#これはNG\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m: 'list' object is not callable"
]
}
],
"source": [
"a = [1, 2, 3]\n",
"a[1] #これはOK\n",
"a(1) #これはNG"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "1NNF7t3XtONA"
},
"source": [
"**0除算**"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "yeZ04wKttTMh"
},
"source": [
"0で割ることはできません"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 168
},
"id": "M3S_PXdFtPVQ",
"outputId": "12364983-d12c-4a1c-d48e-85dc04ad8191"
},
"outputs": [
{
"ename": "ZeroDivisionError",
"evalue": "ignored",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mZeroDivisionError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;36m300\u001b[0m\u001b[0;34m/\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mZeroDivisionError\u001b[0m: division by zero"
]
}
],
"source": [
"300/ 0"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "qrDtUKhXtnvo"
},
"source": [
"## 論理エラー\n",
"\n",
"論理エラーとは、文法的にもOK(syntax errorなし)で、ランタイムエラーも無く、最後までプログラムが実行されるが、「意図した通りに動かない」というものを指します。"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "XjepDskttuhm"
},
"source": [
"たとえば「aが負かつ正なら文字列を表示する」という以下のコードは、論理的にはおかしい命令ですが、構文エラーにもランタイムエラーにもなりません\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "w7CuitFBuE5Z"
},
"outputs": [],
"source": [
"a = 2.0\n",
"if a < 0.0 and a > 0.0: \n",
" print(\"aは正かつ負です!!\")"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "9gWAbE1OvIeV"
},
"source": [
"人間側のエラーとも言えそうですね。 \n",
"第1章で説明したように、print文を使ったりしながらこうした論理エラーが無いか調べることもときに必要です。"
]
}
],
"metadata": {
"colab": {
"authorship_tag": "ABX9TyNQPlOipy4Wx6FmhulV/Ewt",
"include_colab_link": true,
"name": "Python_misc_Error.ipynb",
"provenance": []
},
"kernelspec": {
"display_name": "Python 3",
"name": "python3"
}
},
"nbformat": 4,
"nbformat_minor": 0
}