@@ -152,6 +152,68 @@ isn't exactly like mine but it works just fine it's ok, and you can
152
152
print (message, " !!!" )
153
153
```
154
154
155
+ # # Lists and tuples
156
+
157
+ 1 . When we run the program we get a weird error:
158
+
159
+ Hello!
160
+ Enter your name: my name
161
+ Traceback (most recent call last):
162
+ File " program.py" , line 3 , in < module>
163
+ print (" Your name is " + name + " ." )
164
+ TypeError : Can' t convert ' tuple ' object to str implicitly
165
+
166
+ So Python is trying to convert a tuple to a string. But
167
+ `" Your name is " ` and `" ." ` are strings, so maybe `name` is a
168
+ tuple ? Let' s change the last line to just `print(name)` so our
169
+ program looks like this:
170
+
171
+ ```py
172
+ print (" Hello!" )
173
+ name = input (" Enter your name: " ),
174
+ print (name)
175
+ ```
176
+
177
+ Let' s run it.
178
+
179
+ Hello!
180
+ Enter your name: my name
181
+ (' my name' ,)
182
+
183
+ `name` is indeed a tuple ! The problem is the second line. Look
184
+ carefully, there' s a comma after `input("Enter your name: ")`.
185
+ Python created a tuple automatically, but that' s not what we
186
+ wanted. If we remove the comma, everything works just fine.
187
+
188
+ 2 . Again, the code gives us a weird error message.
189
+
190
+ Enter your name: my name
191
+ Traceback (most recent call last):
192
+ File " program.py" , line 3 , in < module>
193
+ if input (" Enter your name: " ) in namelist:
194
+ TypeError : argument of type ' NoneType' is not iterable
195
+
196
+ Now we need to remember that when the error messages talk about
197
+ `NoneType` [they always mean None ](variables.md# none). So
198
+ `namelist` seems to be None . Let' s make the program a bit simpler
199
+ for working on the namelist:
200
+
201
+ ```py
202
+ namelist = [' wub_wub' , ' RubyPinch' , ' go|dfish' , ' Nitori' ]
203
+ namelist = namelist.extend(' theelous3' )
204
+ print (namelist)
205
+ ```
206
+
207
+ Now fixing the namelist is easier, so I' ll just go through the
208
+ problems and solutions:
209
+
210
+ - `namelist` is None . It should be `namelist.extend(' theelous3' )` ,
211
+ not `namelist = namelist.extend(' theelous3' )` .
212
+ - Now the namelist is like `[' wub_wub' , ... , ' t' , ' h' , ' e' , ' e' , ... ]` .
213
+ Python treated `' theelous3' ` like a list so it added each of its
214
+ characters to `namelist` . We can use `namelist.append(' theelous3' )`
215
+ or `namelist.extend([' theelous3' ])` instead to solve this problem.
216
+
155
217
# # Loops
156
218
157
219
1 . For loop over the users, each user is a list that contains a
0 commit comments