I'm trying to solve Ex 41 from Zed Shaw's "Learn Python the Hard Way". I have created the file for it. The exercise retrieves a text file from the author's website. When I run it on the command line (Ubuntu) it just kicks me back to the prompt with no visible output.
I'm not sure how to identify what the problem is. I have tried:
Checking the code carefully. The code looks identical to the book as far as I can tell.
Running it in IDLE instead, produces no output, just returns to the prompt
Running python with -v (which didn't produce anything)
Changing the URL to https, and
Verifying that the word list is available at that URL (it is).
Any other python exercise I have still runs fine. Is there a way to see more output (like a log file, or a method of forcing more output) where I can try to figure out what is going on?
import random
from urllib import urlopen
import sys
WORD_URL = "https://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
PHRASE_FIRST = False
if len(sys.argv) == 2 and sys.argv[1] == "english":
PHRASE_FIRST = True
#load up 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 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(results)
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"