justinlee799 / pwp-capstones

0 stars 0 forks source link

How to create a variable containing a TextSample object #5

Open jmcrey opened 6 years ago

jmcrey commented 6 years ago
murder_sample = TextSample()
lily_sample = TextSample()
myrtle_sample = TextSample()
gregg_sample = TextSample()

print(murder_sample)
print(lily_sample)
print(myrtle_sample)
print(gregg_sample)

Again, this code is so close to being 100% correct! All we need to add to these four variables are the required parameters for the TextSample class to be initialized. We can find what variables are required by looking at the __init__ function of the TextSample class. For example, our __init__ function is defined as the following:

def __init__(self, text, author):

By observing this function signature (that's what these kinds of statements are called), we know that we need to give the TextSample class a text and an author for it to be initialized properly (note: ignore the self parameter).

Now that we know what parameters need to be passed, all we have to do is include those when we make our call. So, for the murder_sample variable, we can do the following:

murder_sample = TextSample(muder_note, 'Murderer')

Note that the murder_note variable was defined earlier in our program (in fact, it was the very first line!). When this line executes, the variable murder_note and string 'Murderer will be passed to the __init__ function of our TextSample class like so:

__init__(self, murder_note, 'Murderer') again, ignore self

Then these would be passed down like any other function call we make. Note that the 'Murderer' string is simply the name of the person who authored that text. So, If we were to define the lily_sample variable, instead of 'Murderer' we might give it the name 'Lily Trebuchet' since that is her full name. Here is the full example for Lily:

lily_sample = TextSample(lily_trebuchet_intro, 'Lily Trebuchet')

Again, lily_trebuchet_intro was defined earlier in our program.

For practice, try to go back and add in these parameters for the myrtle_sample and gregg_sample variables. If you have any questions, please let me know!

justinlee799 commented 6 years ago

i keep getting an error saying "Name Error: name 'TextSample' is not defined