0

http://learnpythonthehardway.org/book/ex41.html

How I run the script (exactly same as the writer)

import random
from urllib 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 *** parameters.",
  "class %%%(object):\n\tdef ***(self, @@@)":
    "class %%% has-a function named *** that takes self and @@@ parameters.",
  "*** = %%%()":
    "Set *** to an instance of class %%%.",
  "***.***(@@@)":
    "From *** get the *** function, and call it with parameters 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(word.strip())


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:
      result = sentence[:]

      # 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 = 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

              raw_input("> ")
              print "ANSWER:  %s\n\n" % answer
except EOFError:
    print "\nBye"

Then I get this error (the picture)? Is this error concerned with my python's edition?

Because I even use CTRL-C CTRL-V to ensure the script is exactly same as the writer's,so I can't figure out what cause the error?

I'm using Python 2.7.6 (default, Sep 9 2014, 15:04:36)
enter image description here

The error:

Traceback (most recent call last):
  File "123.py", line 30, in <module>
    for word in urlopen(WORD_URL).readlines():
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib.py", line 87, in urlopen
    return opener.open(url)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib.py", line 208, in open
    return getattr(self, name)(url)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib.py", line 346, in open_http
    errcode, errmsg, headers = h.getreply()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 1117, in getreply
    response = self._conn.getresponse()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 1045, in getresponse
    response.begin()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 409, in begin
    version, status, reason = self._read_status()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 365, in _read_status
    line = self.fp.readline(_MAXLINE + 1)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 476, in readline
    data = self._sock.recv(self._rbufsize)
IOError: [Errno socket error] [Errno 54] Connection reset by peer
musiKk
  • 14,751
  • 4
  • 55
  • 82
t4tao
  • 27
  • 5
  • 1
    Which picture? Don't include a screengrab of the error. Its best to include the actual text. – Paul Rooney Mar 10 '15 at 06:37
  • 1
    Reference: http://stackoverflow.com/a/20568874/1734707 – planet260 Mar 10 '15 at 06:45
  • Can you access [the URL](http://learncodethehardway.org/words.txt) through your browser without issue? – jedwards Mar 10 '15 at 06:46
  • 1
    If I cut & paste it it works no issues. I think you may have some indentation problems. Although I can't be sure until you post the error. For one thing look how the first half of the code (before try while true) is indented in and the rest is not. – Paul Rooney Mar 10 '15 at 07:11
  • I can access the URL through my computer – t4tao Mar 10 '15 at 08:50
  • and the I have included the actual text of error. – t4tao Mar 10 '15 at 08:50
  • can anybody upload the supposed answer for me ??? don't want to miss this exercise. – t4tao Mar 10 '15 at 12:06
  • It's almost certainly not related to your Python version. It's either a simple copy+paste error -- even though you tried to be careful -- or a network problem on your end. – tripleee Mar 10 '15 at 12:32
  • Uploading the "answer" here would seem kind of pointless. The purpose of an exercise is for you to complete it yourself and learn something from the experience. – tripleee Mar 10 '15 at 12:35

0 Answers0