teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

2

追記

2019/05/21 03:51

投稿

LouiS0616
LouiS0616

スコア35678

answer CHANGED
@@ -26,4 +26,33 @@
26
26
  1
27
27
  2
28
28
  >>>
29
+ ```
30
+
31
+ ```Python
32
+ >>> import random
33
+ >>>
34
+ >>> foods = [{'country': 'chinese', 'type': 'noodle'},
35
+ ... {'country': 'japanese', 'type': 'sushi'},
36
+ ... {'country': 'japanese', 'type': 'noodle'},
37
+ ... {'country': 'american', 'type': 'hamburger'}]
38
+ >>>
39
+ >>> for e in range(1, 3), foods:
40
+ ... print(f'e is {e}')
41
+ ... a, b = e
42
+ ... print(f'a is {a}')
43
+ ... print(f'b is {b}')
44
+ ... b[0]
45
+ ...
46
+ e is range(1, 3)
47
+ a is 1
48
+ b is 2
49
+ Traceback (most recent call last):
50
+ File "<stdin>", line 6, in <module>
51
+ TypeError: 'int' object is not subscriptable
52
+ >>>
53
+ >>> 2[0]
54
+ Traceback (most recent call last):
55
+ File "<stdin>", line 1, in <module>
56
+ TypeError: 'int' object is not subscriptable
57
+ >>>
29
58
  ```

1

追記

2019/05/21 03:51

投稿

LouiS0616
LouiS0616

スコア35678

answer CHANGED
@@ -8,4 +8,22 @@
8
8
  ```Python
9
9
  for i, food_information in enumerate(random.sample(foods, 2), start=1):
10
10
  ...
11
+ ```
12
+
13
+ なぜエラーが出たか?
14
+ ---
15
+ A. 単に並べて書くと、タプルとして扱われるから。
16
+ ```Python
17
+ >>> for e in 1, 2:
18
+ ... print(e)
19
+ ...
20
+ 1
21
+ 2
22
+ >>> tpl = (1, 2)
23
+ >>> for e in tpl:
24
+ ... print(e)
25
+ ...
26
+ 1
27
+ 2
28
+ >>>
11
29
  ```