回答編集履歴

2

追記

2019/05/21 03:51

投稿

LouiS0616
LouiS0616

スコア35660

test CHANGED
@@ -55,3 +55,61 @@
55
55
  >>>
56
56
 
57
57
  ```
58
+
59
+
60
+
61
+ ```Python
62
+
63
+ >>> import random
64
+
65
+ >>>
66
+
67
+ >>> foods = [{'country': 'chinese', 'type': 'noodle'},
68
+
69
+ ... {'country': 'japanese', 'type': 'sushi'},
70
+
71
+ ... {'country': 'japanese', 'type': 'noodle'},
72
+
73
+ ... {'country': 'american', 'type': 'hamburger'}]
74
+
75
+ >>>
76
+
77
+ >>> for e in range(1, 3), foods:
78
+
79
+ ... print(f'e is {e}')
80
+
81
+ ... a, b = e
82
+
83
+ ... print(f'a is {a}')
84
+
85
+ ... print(f'b is {b}')
86
+
87
+ ... b[0]
88
+
89
+ ...
90
+
91
+ e is range(1, 3)
92
+
93
+ a is 1
94
+
95
+ b is 2
96
+
97
+ Traceback (most recent call last):
98
+
99
+ File "<stdin>", line 6, in <module>
100
+
101
+ TypeError: 'int' object is not subscriptable
102
+
103
+ >>>
104
+
105
+ >>> 2[0]
106
+
107
+ Traceback (most recent call last):
108
+
109
+ File "<stdin>", line 1, in <module>
110
+
111
+ TypeError: 'int' object is not subscriptable
112
+
113
+ >>>
114
+
115
+ ```

1

追記

2019/05/21 03:51

投稿

LouiS0616
LouiS0616

スコア35660

test CHANGED
@@ -19,3 +19,39 @@
19
19
  ...
20
20
 
21
21
  ```
22
+
23
+
24
+
25
+ なぜエラーが出たか?
26
+
27
+ ---
28
+
29
+ A. 単に並べて書くと、タプルとして扱われるから。
30
+
31
+ ```Python
32
+
33
+ >>> for e in 1, 2:
34
+
35
+ ... print(e)
36
+
37
+ ...
38
+
39
+ 1
40
+
41
+ 2
42
+
43
+ >>> tpl = (1, 2)
44
+
45
+ >>> for e in tpl:
46
+
47
+ ... print(e)
48
+
49
+ ...
50
+
51
+ 1
52
+
53
+ 2
54
+
55
+ >>>
56
+
57
+ ```