0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Python初心者向け】間違い探し10問(Lv.1)|エラーをゲーム感覚で学ぶ!

0
Posted at

Pythonコード 間違い探し10問(Lv.1)

さっそく始めましょう!

第一問

if True
    print("OK")
答え

if True のあとに : がありません

解説

if while for などのあとには : が必要です

修正例
if True :
    print("OK")

第二問

array = [ 10, 20, 30 ]
print(array[3])
ヒント

リストの要素の取得方法に注目!

答え

指定するインデックス(位置)が範囲外です

解説

array の長さ(要素数)は 3 であるのに対して、インデックスが 3 という範囲外へのアクセスを試みています

IndexError が発生します

ポイント

インデックス(添え字)は 0 から始まります

修正例
array = [ 10, 20, 30 ]
print(array[2]) # 範囲内の数値にする

第三問

name = "hello world"
print(Name)
答え

Name ではなく name

解説

大文字と小文字は区別されます

修正例
name = "hello world"
print(name)

第四問

if x == 10 :
    print("OK")
else :
     print("?")
ヒント

何かズレてる気が...

答え

4行目のインデントがずれています

ポイント

インデントは半角スペース4つでそろえましょう

修正例
if x == 10 :
    print("OK")
else :
    print("?")

第五問

prant("Hello World!")
ヒント

スペルミスしちゃった...

答え

prant という関数はデフォルトではありません

解説

たぶん、print 関数かな?

修正例
print("Hello World!")

第六問

num_apple = 10  # りんごの個数

print("りんごの個数は" + num_apple + "個ですか?")
ヒント

num_apple は数値?

答え

"りんごの~" という文字列と num_apple という数値を足しています

解説

異なるデータ型の演算はできません(たいていの場合)

修正例
num_apple = 10  # りんごの個数

print("りんごの個数は" + str(num_apple) + "個ですか?")

第七問

text = "こんにちは'
答え

"' で閉じています

解説

文字列では同じ記号("')で挟まれている必要があります

修正例
text = 'こんにちは'

第八問

if x = 10 :
    print("OK")
ヒント

第一問では if True だったけど...

答え

x = 10 は真偽値ではありません

解説

= は代入演算子で(右辺)の値を(左辺)に入れます
== は比較演算子で(左辺)と(右辺)を比較して、真偽値を返します

修正例
if x == 10 :
    print("OK")

第九問

def hello() :
    print("ハロー")

hello
答え

hello のあとに () がありません

解説

関数を呼び出すときは () をつけます
引数は () の中に入れます

修正例
def hello() :
    print("ハロー")

hello()

第十問

dictionary = { "0" : "山田太郎", "1" : "山田次郎" }
print(dictionary[0])
答え

dictionary[0] のキーは存在しません

ポイント

キーは "0"0 とで区別されます

修正例
dictionary = { "0" : "山田太郎", "1" : "山田次郎" }
print(dictionary["0"])

あとがき

簡単すぎましたか?
何個か作ってみます

ありがとうございました

0
1
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?