Skip to content

Commit 4a22918

Browse files
committed
add more exercises
1 parent d2c1980 commit 4a22918

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed

answers.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,68 @@ isn't exactly like mine but it works just fine it's ok, and you can
152152
print(message, "!!!")
153153
```
154154

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+
155217
## Loops
156218

157219
1. For loop over the users, each user is a list that contains a

lists-and-tuples.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,18 @@ used in places like `(1 + 2) * 3`.
266266
>>>
267267
```
268268

269+
It's also possible to create tuples by just separating things with
270+
commas and adding no parentheses. Personally I don't like this feature,
271+
but some people like to do it this way.
272+
273+
```py
274+
>>> 1, 2, 3
275+
(1, 2, 3)
276+
>>> 'hello',
277+
('hello',)
278+
>>>
279+
```
280+
269281
Tuples don't have methods like append, extend and remove
270282
because they can't change themselves in-place.
271283

@@ -310,6 +322,29 @@ else:
310322
print("Sorry, I don't know who you are :(")
311323
```
312324

325+
## Exercises
326+
327+
1. Fix this program.
328+
329+
```py
330+
print("Hello!")
331+
name = input("Enter your name: "),
332+
print("Your name is " + name + ".")
333+
```
334+
335+
2. Fix this program.
336+
337+
```py
338+
namelist = ['wub_wub', 'RubyPinch', 'go|dfish', 'Nitori']
339+
namelist = namelist.extend('theelous3')
340+
if input("Enter your name: ") in namelist:
341+
print("I know you!")
342+
else:
343+
print("I don't know you.")
344+
```
345+
346+
The answers are [here](answers.md#lists-and-tuples).
347+
313348
***
314349

315350
You may use this tutorial freely at your own risk. See

0 commit comments

Comments
 (0)