0

I get an error when executing the example snippet of exercise 41 in the book 'Learning Python the hard way' of ZED A. SHAW. .

The error I am receiving: Traceback (most recent call last):

File "oop_test.py", line 75, in <module>
question, answer = convert(snippet, phrase)
File "oop_test.py", line 48, in convert
result = sentence.copy()
AttributeError: 'str' object has no attribute 'copy'

The snippet:

import random
from urllib.request import urlopen
import sys

WORD_URL = "http://learncodethehardway.org/words.txt"
WORDS = []

PHRASES = {
    "class %%%(%%%):":
      "Make a class named %%% that is-a %%%.",
    "class %%%(object):\n\tdef __init__(self, ***)" :
      "class %%% has-a __init__ that takes self and *** params.",
    "class %%%(object):\n\tdef ***(self, @@@)":
      "class %%% has-a function *** that takes self and @@@ params.",
    "*** = %%%()":
      "Set *** to an instance of class %%%.",
    "***.***(@@@)":
      "From *** get the *** function, call it with params self, @@@.",
    "***.*** = '***'":
      "From *** get the *** attribute and set it to '***'."
}

# do they want to drill phrases first
if len(sys.argv) == 2 and sys.argv[1] == "english":
    PHRASE_FIRST = True
else:
    PHRASE_FIRST = False

# load up the words from the website
for word in urlopen(WORD_URL).readlines():
    WORDS.append(str(word.strip(), encoding="utf-8"))


def convert(snippet, phrase):
    class_names = [w.capitalize() for w in
                   random.sample(WORDS, snippet.count("%%%"))]
    other_names = random.sample(WORDS, snippet.count("***"))
    results = []
    param_names = []

    for i in range(0, snippet.count("@@@")):
        param_count = random.randint(1,3)
        param_names.append(', '.join(
            random.sample(WORDS, param_count)))

    for sentence in snippet, phrase:
        # this is how you duplicate a list or string
        result = sentence.copy()

        # fake class names
        for word in class_names:
            result = result.replace("%%%", word, 1)

        # fake other names
        for word in other_names:
            result = result.replace("***", word, 1)

        # fake parameter lists
        for word in param_names:
            result = result.replace("@@@", word, 1)

        results.append(result)

    return results


# keep going until they hit CTRL-D
try:
    while True:
        snippets = list(PHRASES.keys())
        random.shuffle(snippets)

        for snippet in snippets:
            phrase = PHRASES[snippet]
            question, answer = convert(snippet, phrase)
            if PHRASE_FIRST:
                question, answer = answer, question

            print(question)

            input("> ")
            print(f"ANSWER:  {answer}\n\n")
except EOFError:
    print("\nBye")

It looks like it isn't interpreted as a dictionary or list? I don't really understand why the copy method isn't working? I 'solved' it by removing the .copy on line 48 and just use 'result = sentence' but I guess that isn't properly fixed and there was a reason the author used .copy ?

user3339208
  • 71
  • 1
  • 1
  • 12
  • What do you think `for sentence in snippet, phrase` is doing? – Daniel Roseman Sep 09 '17 at 21:26
  • I guess it is iterating over the items in the snippets variable (which is a list of the keys of the PHRASES variable which is a dictionary with for example "class %%%(%%%):" as key and "Make a class named %%% that is-a %%%." as the corresponding value". Following the convert function is called with snippet and phrase as arguments (for example: convert(class %%%(object): def __init__(self, ***), class Art has-a __init__ that takes self and brick params.) . Did you asked this to show me the convert function is receiving a string? Which is the reason the copy() function can't be used? Thank you! – user3339208 Sep 09 '17 at 21:43
  • Well no, it is not doing that. Try commenting out the rest of the code and just print what sentence is in each iteration. – Daniel Roseman Sep 09 '17 at 21:51
  • I guess for each iteration it is printing for example a line: [ ***.*** = '***' ](=answer) and a line [ *** get the *** attribute and set it to '***'. ] (=question). and appends those to the results variable after the replace functions are used on those? (which in case of the given example above will contain the following list: ["authority.bomb = 'burst'", "From authority get the bomb attribute and set it to 'burst'."]) ? – user3339208 Sep 09 '17 at 22:08
  • @user3339208. Instead of guessing, do something like this: `print(repr(snippet), type(snippet))`. Then you can see what the current value/type of a variable actually is. – ekhumoro Sep 09 '17 at 22:10
  • Thank you, I used print(repr(sentence), type(sentence)) which indicates the sentence/result is of type 'str' . May I assume that this is the reason the copy method doesn't work on it? Or am I making a mistake/ is there another reason? – user3339208 Sep 09 '17 at 22:24
  • @user3339208. Yes, only mutable sequence types such as `dict`, `list`, `set`, etc have a `copy()` method. The `str` type is immutable, so it doesn't need one. – ekhumoro Sep 10 '17 at 13:30

1 Answers1

0

Just stumbled across this, in my version of the book the line is

result = sentence [:]

Hope that helps

Michal Foc
  • 13
  • 1
  • 3