I manually typed all the code out to match Zed’s to the best of my ability: In Powershell Im getting an error:
PS C:\Users\jharr\lpthw> python oop_test.py english Traceback (most recent call last):
File “oop_test.py”, line 73, in
question, answer = convert(snippet, phrase)
ValueError: not enough values to unpack (expected 2, got 1)
It seems its not returning but one value. Also, I dont understand in the function why two lists are created but only one of them are returned? Ill paste the whole script with lines for reference. If anyone is using Atom, is there a debugger or a way to step through the script as it compiles so I can watch what list isnt getting built? I think thats the problem, but I cant be sure. I need to see whats going on. Ex41(oop_test.py):
import random
2 from urllib.request import urlopen
3 import sys
4
154 LEARN PYTHON 3 THE HARD WAY
5 WORD_URL = “http://learncodethehardway.org/words.txt”
6 WORDS = []
7
8 PHRASES = {
9 “class %%%(%%%):”:
10 “Make a class named %%% that is-a %%%.”,
11 “class %%%(object):\n\tdef init(self, )" :
12 “class %%% has-a init that takes self and *** params.”,
13 “class %%%(object):\n\tdef (self, @@@)":
14 “class %%% has-a function *** that takes self and @@@ params.”,
15 " = %%%()”:
16 “Set *** to an instance of class %%%.”,
17 “***.***(@@@)”:
18 “From *** get the *** function, call it with params self, @@@.”,
19 "***. = ‘***’”:
20 “From *** get the *** attribute and set it to ‘***’.”
21 }
22
23 # do they want to drill phrases first
24 if len(sys.argv) == 2 and sys.argv[1] == “english”:
25 PHRASE_FIRST = True
26 else:
27 PHRASE_FIRST = False
28
29 # load up the words from the website
30 for word in urlopen(WORD_URL).readlines():
31 WORDS.append(str(word.strip(), encoding=“utf-8”))
32
33
34 def convert(snippet, phrase):
35 class_names = [w.capitalize() for w in
36 random.sample(WORDS,
snippet.count("%%%"))]
37 other_names = random.sample(WORDS,
snippet.count("***"))
38 results = []
39 param_names = []
40
41 for i in range(0, snippet.count("@@@")):
42 param_count = random.randint(1,3)
43 param_names.append(’, '.join(
44 random.sample(WORDS, param_count)))
45
46 for sentence in snippet, phrase:
47 result = sentence[:]
48
49 # fake class names
50 for word in class_names:
51 result = result.replace("%%%", word, 1)
52
53 # fake other names
54 for word in other_names:
55 result = result.replace("***", word, 1)
56
57 # fake parameter lists
58 for word in param_names:
59 result = result.replace("@@@", word, 1)
60
61 results.append(result)
62
63 return results
64
65
66 # keep going until they hit CTRL-D
67 try:
68 while True:
69 snippets = list(PHRASES.keys())
70 random.shuffle(snippets)
71
72 for snippet in snippets:
73 phrase = PHRASES[snippet]
74 question, answer = convert(snippet, phrase)
75 if PHRASE_FIRST:
76 question, answer = answer, question
77
78 print(question)
79
80 input("> “)
81 print(f"ANSWER: {answer}\n\n”)
82 except EOFError:
83 print("\nBye")
I have more questions about this script, but Im just trying to get it to run properly first. Thanks in advance.