Stuck on Ex.41 - can't figure out how to run

I have typed everything exactly as it is written in the e-book in correctly into oop.py, however, when I run it in in the windows powershell, I get nothing. I’ve copied it below. Isn’t the program supposed to pop out phrases for me to practice on/with? I’m not sure how to get this to interact with me in the powershell.

PS C:\Users\bloo\desktop\pythonprobs> python oop_test.py
PS C:\Users\bloo\desktop\pythonprobs>

I guess I’m not sure how oop_test.py and ex41 are supposed to be used together? If we put the code in oop_test.py, what goes in ex41.py? I’m very, very confused on how to get this running - I already had to make a proxy because of my computer’s configuration. This is probably a very dumb question, for which I apologize, I’m just lost.
I was steadily following this book up until exercise 41 a few months ago (because then I got very, very busy), and even when all the material was fresh in my mind I was still confused because classes are just hard to wrap my head around. Any help is greatly appreciated!

Hello and welcome to the forum @bloo.

I also thought so in the beginning. Looking at the code until it felt like my eyes was going to pop out.
But. There is always something wrong. Or missing.
If you share your code more eyes can have a look and help you. Best thing to do is:


[code]
Paste your code in here
[/code]


You perhaps find it useful to have a look at another thread in this forum about this exercise: ”help needed with ex41” >> Link to thread

Here’s the code - Sorry, I should have posted this to begin with. Thanks for calling that out.

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.captialize() 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 class_names
        for word in other_names:
            results = 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"

Let me see, can you explain this to me:

" already had to make a proxy because of my computer’s configuration."

Now, for the code, you can always find the code for every exercise here:

And for that exercise it’s:

And you can get the raw text here:

https://raw.githubusercontent.com/zedshaw/learn-python3-thw-code/master/ex41.py

Then, you can just grab that and run it and forget about fixing what you did. Or, you can see if you really did type it exactly the same by doing this:

diff zeds_ex41.py oop_test.py

That assumes the ex41.py that I linked above is named zeds_ex41.py, and your file is named oop_test.py.

But, this will only work on Linux or OSX. What OS are you using?

Using the diff method, I see that I didn’t actually type in exactly the same - my guess is that I didn’t use the right spaces or indents. However, I’m still confused on how ex41.py and oop_test.py are related - are they actually the same code in two different files?

I had to use a proxy because of the way my computer is configured - I shot the error message I was getting from originally running to a coworker of mine who is already familiar with Python, and he showed me how to do a proxy in powershell. I have no idea how the configuration of my computer means that I need a proxy, I just know I emailed him about it, and I emailed the help@learncodethehardway.org and received input that my computer config or something along those lines was to blame for the specific error message.

When I ran the zeds_ex41.py, I still got an error in powershell that had to do with an EOL error, which was weird because I literally copied and pasted that earlier txt file and saved it as zeds_ex41.py.

I just realized that the current code in the LPTHW txt file is from Learn Python 3 the Hard Way - there is at least one difference between this code and the code in the ebook for Learn Python 2 the Hard Way - I’m thinking that my problems may be coming from not learning the most recent version of Python and comparing my work to the most recent txt versions provided. All that to say I’m probably going to start over with learning Python 3 and am giving up on this exercise for now - there’s so many random things going wrong with it I’m probably better off starting from scratch I think.

To add: I just remembered the proxy was necessary because I couldn’t load up words from the website.

Thank you both for your help and patience.

The file “ex41.py” is what I call it in my build of the book, but people were confused so I named it oop_test.py. It’s actually irrelevant what you call it. You could call it akjhasdfhkjasdfhnlasdflhkasdfkhjasdf.py and it would still run.

Now, you say that you have python2 version of the book, and are using Python 3 on your computer?

Can you do this:

python --version

And paste that back here. Also, at some point you should just run my ex41.py file and use that to practice the terminology.

A free service run by Zed A. Shaw for learncodethehardway.org.