Susan Harkins (388) [Avatar] Offline
#1
Please post errors found in the published version of Get Programming here. If necessary, we'll publish a comprehensive list for everyone's convenience. Thank you!

Regards,
Susan Harkins
Errata Editor
Manning Publications
589288 (3) [Avatar] Offline
#2
In the answer to Q16.1 it is suggested that you cannot code multiple prints of 'Hello' without a for loop.
However, if you use a multiplication of strings syntax, you can:
if num is the number of times given through input, your code print(num * 'Hello\n') will do the job without a loop.

(In the answer to Q17.3 the last two lines of code to print the last name in a given row of names, can be simplified to:
print('Hi ' + name) because the variable name has stored the last name already. No need to use names.rfind(" ").)
589288 (3) [Avatar] Offline
#3
Hi,

On page 278, Listing 28.1 Function that mutates a dictionary, it states in the code:

if word in d:
d[word].append(definition)

This gives me the error message: AttributeError: 'str' object has no attribute 'append'.

The string is the value of the key d[word]. How does Python "know" to make a list if a key has more than one value?

I solved it by opening an empty list and appending the existing value and 'definition' to it and then assigning the list to d[word].

if word in d:
v = [ ]
v.append(d[word])
v.append(definition)
d[word] = v

(Sorry, can't get indentations working in this format)

589288 (3) [Avatar] Offline
#4
Hi,

On page 283 the answer to Question 28.2 and Listing 28.4, it is suggested that you can empty a list or a dictionary, after which you are still working with the same list/dictionary because they are mutable objects.

If I do this with dict = { } and then check this with id(dict) I get different memory slots for the original dict and its emptied state.
Ana Bell (29) [Avatar] Offline
#5
I am wondering if there is a part of the code missing when you typed it in, which led you to get that error. The code should be:

def add_word(d, word, definition):
    if word in d:
        d[word].append(definition)
    else:
        d[word] = [definition] # note the square brackets around definition


The important part is inside the else code block. If there is no list for the value of that key, then you create a new list with one element inside it, the first definition to add. So the line
d[word] = [definition] 
is equivalent to these two lines:

d[word] = [] 
d[word].append(definition)


For future reference, add code by clicking the green button with code on it, then type/paste the code, then click the green button with code again.

Hope this makes sense!

589288 wrote:Hi,

On page 278, Listing 28.1 Function that mutates a dictionary, it states in the code:

if word in d:
d[word].append(definition)

This gives me the error message: AttributeError: 'str' object has no attribute 'append'.

The string is the value of the key d[word]. How does Python "know" to make a list if a key has more than one value?

I solved it by opening an empty list and appending the existing value and 'definition' to it and then assigning the list to d[word].

if word in d:
v = [ ]
v.append(d[word])
v.append(definition)
d[word] = v

(Sorry, can't get indentations working in this format)

Ana Bell (29) [Avatar] Offline
#6
You are absolutely right. The code in both of these cases does not mutate the original list/dict. Instead, it makes the variable name point to a new empty list/dict. The original list/dict still exists somewhere in memory, we've just lost the handle to it.


589288 wrote:Hi,

On page 283 the answer to Question 28.2 and Listing 28.4, it is suggested that you can empty a list or a dictionary, after which you are still working with the same list/dictionary because they are mutable objects.

If I do this with dict = { } and then check this with id(dict) I get different memory slots for the original dict and its emptied state.
Susan Harkins (388) [Avatar] Offline
#7
The current errata list for Get Programming is available at https://manning-content.s3.amazonaws.com/download/0/217846f-8d8f-4409-8f8d-4d1ed0c665b9/Bell_GetProgramming_LearntocodewithPython_err1.html. Thanks to all who participate in this process!

Regards,
Susan Harkins
Errata Editor
479901 (1) [Avatar] Offline
#8
Section 5.2.3 says, "...the expression 4 < 5 is replaced by False."