Jump to content
talkfootball365
  • Welcome to talkfootball365!

    The better place to talk football.

Python Programming


football forum

Recommended Posts

8 hours ago, Mel81x said:

 


def highest_turnout(data) :
  highest_turnout = data[0] # assign the first object as the highest this contains everything in it
  for county in data :
	  if int(county.turnout) > int(highest_turnout.turnout) : #I match the turnout values using INT which could be made better but its good for demo purposes. In here you can see the county which is the data iterator on the list - data and the highest_turnout object 
		  highest_turnout = county #once i know its the highest I just take the full object and assign it to highest turnout

  return highest_turnout

 

So here is where I want to dive in a little deeper. I have added my own comments in RED. 

def highest_turnout(data) :
  highest_turnout = data[0] # makes sense, and I had this at one point. However, I was thinking that it would pull the first variable in the section of Data, which would be the county name. So in this case the output would be allegheny since it's the first instance. 
  for county in data : These lines of code confuse me too. I'm assuming you are referring here to NAME of the data, and not actually the county name? 
      if int(county.turnout) > int(highest_turnout.turnout) : No complaints here, this all makes sense. 
          highest_turnout = county - Again, I'm confused by the county statement. To me, county = the name, population, and the number of voters. My projects expects me to print a list with the County's name, and percentage of voters, and I'm not sure who this does that. 

  return highest_turnout

Link to comment
Share on other sites

  • Replies 160
  • Created
  • Last Reply
  • Subscriber
36 minutes ago, Eco said:

So here is where I want to dive in a little deeper. I have added my own comments in RED. 

def highest_turnout(data) :
  highest_turnout = data[0] # makes sense, and I had this at one point. However, I was thinking that it would pull the first variable in the section of Data, which would be the county name. So in this case the output would be allegheny since it's the first instance. 
  for county in data : These lines of code confuse me too. I'm assuming you are referring here to NAME of the data, and not actually the county name? 
      if int(county.turnout) > int(highest_turnout.turnout) : No complaints here, this all makes sense. 
          highest_turnout = county - Again, I'm confused by the county statement. To me, county = the name, population, and the number of voters. My projects expects me to print a list with the County's name, and percentage of voters, and I'm not sure who this does that. 

  return highest_turnout

Lets talk about this first

These lines of code confuse me too. I'm assuming you are referring here to NAME of the data, and not actually the county name? 

What I am doing in that loop is assigning the entire country to a variable and not just the turnout so that at the end I can return the turnout value without having to worry about assigning a turnout value later on as thats just another assignment we don't need. You'll see why when I show you the error in your code.

Again, I'm confused by the county statement. To me, county = the name, population, and the number of voters. My projects expects me to print a list with the County's name, and percentage of voters, and I'm not sure who this does that. 

Over here I set the entire county again because it contains all the data I need to make one calculation wherever I want it. Think of it this way.

County = NAME | POPULATION | VOTERS

So, if I look at just the turnaround then I have to go back and get the county again when I need the name or the percentage of voters right? Instead, what if i stored the entire county and then I can do all the calculations in one place? Makes it all easier since the country has the turnaround and I can just validate and match that value and when I am ready set that county so that any operations later are much faster and I dont have to go scouring a list ever again.

46 minutes ago, Eco said:

First off, thank you @Mel81x and @nudge on your help. Please understand that I'm greatly appreciative. 

So here is what I have right now. However, I'm still getting errors messages about ''county' and others being 'undefined'. 

 


class County:
        def __init__(self, init_name, init_population, init_voters):
                name= init_name
                population = init_population
                voters = init_voters
                turnout = init_population // init_voters #this is my own idea, as I'm trying to add the value for the variable that we will use to deteremine the voter turnout. 


  def highest_turnout(data) :
    highest_turnout = data[0].turnout
    for county in data:
            if int(county.turnout) > int(highest_turnout):
                highest_turnout = county


    return highest_turnout #I continue to get errors from this code stating that county and highest_turnout are not defined, however I keep watching videos and I can't understand why they aren't. 

tup1 = (county, highest_turnout)
print (tup1) #My first poor attempt at a tuple. Idealy, this would certainly give the information that Penn is looking for, however the erros messages I'm getting in visual studios is that 'county' is undefined. 

# your program will be evaluated using these objects 
# it is okay to change/remove these lines but your program
# will be evaluated using these as inputs
allegheny = County("allegheny", 1000490, 645469)
philadelphia = County("philadelphia", 1134081, 539069)
montgomery = County("montgomery", 568952, 399591)
lancaster = County("lancaster", 345367, 230278)
delaware = County("delaware", 414031, 284538)
chester = County("chester", 319919, 230823)
bucks = County("bucks", 444149, 319816)
data = [allegheny, philadelphia, montgomery, lancaster, delaware, chester, bucks]  

result = highest_turnout(data) # do not change this line!
print(result) # prints the output of the function
# do not remove this line!

 

Now for this, lets look at the DEF for highest_turnout again.

def highest_turnout(data):
    highest_turnout = data[0].turnout #we dont know the data type here but it should be string
    for county in data:
        if int(county.turnout) > int(highest_turnout): #this is where syntax can start playing tricks. What you're doing here is converting highest_turnout and county.turnout to INT so no matter what they were before they are now INTs
        
            highest_turnout = county #this is where the compiler and linker will start throwing errors. What you're trying to do here is assign a class to an object that is a string so what you really should do is "highest_turnout = count.turnout"

    return highest_turnout #if you stick to the old code then the compiler is confused here because this object started as a string now its a class

Link to comment
Share on other sites

  • Subscriber
Just now, Eco said:

Okay, let me digest this. 

Thanks a million good sir. 

Anytime when you want to go over your last piece of code for the Tuple let me know because the reason its throwing county as an error is because "county" has really never been defined anywhere except inside functions and the main program has no way of accessing whats in a function.

Link to comment
Share on other sites

  • Subscriber

 

8 hours ago, Eco said:

First off, thank you @Mel81x and @nudge on your help. Please understand that I'm greatly appreciative. 

So here is what I have right now. However, I'm still getting errors messages about ''county' and others being 'undefined'. 

 

class County:
        def __init__(self, init_name, init_population, init_voters): #since you are using the self parameter, you should also use it when defining the attributes
                name= init_name
                population = init_population
                voters = init_voters
                turnout = init_population // init_voters #this is my own idea, as I'm trying to add the value for the variable that we will use to deteremine the voter turnout.  

  def highest_turnout(data) :
    highest_turnout = data[0].turnout 
    for county in data: #capital letters matter! Your class is County, you are trying to loop with county (which is why it comes back with not defined error), check for it in your whole code  #DISREGARD THIS!
            if int(county.turnout) > int(highest_turnout):#why integers? You surely want to compare floats as it has to be a number between 0 and 1?
                highest_turnout = county #exactly here you have changed your variable type! Initially, you defined highest_turnout as a number (i.e. turnout, calculated from voters/population), but here you are changing it to the County object! Hence the errors. For the next step in loop, your highest_turnout variable suddenly is of different type and has more attributes (as it's now a County object) and the code breaks...

# also as Mel already said, you are better off comparing the whole County objects here in the highest_turnout function instead of just one attribute (turnout), as they contain all the data you need - especially since you need the output to be the County name AND the turnout percentage - whereas the approach you have here will only return the turnout percentage, but not the County name!
    return highest_turnout #I continue to get errors from this code stating that county and highest_turnout are not defined, however I keep watching videos and I can't understand why they aren't. 

tup1 = (county, highest_turnout) #this will give you errors too, as County is not a defined variable, it's a class. Fix the previous function first and then work on this. Hint: you can get the highest_turnout function to return you a tuple of the County name and turnout automatically.
print (tup1) #My first poor attempt at a tuple. Idealy, this would certainly give the information that Penn is looking for, however the erros messages I'm getting in visual studios is that 'county' is undefined. 

# your program will be evaluated using these objects 
# it is okay to change/remove these lines but your program
# will be evaluated using these as inputs
allegheny = County("allegheny", 1000490, 645469)
philadelphia = County("philadelphia", 1134081, 539069)
montgomery = County("montgomery", 568952, 399591)
lancaster = County("lancaster", 345367, 230278)
delaware = County("delaware", 414031, 284538)
chester = County("chester", 319919, 230823)
bucks = County("bucks", 444149, 319816)
data = [allegheny, philadelphia, montgomery, lancaster, delaware, chester, bucks]  

result = highest_turnout(data) # do not change this line!
print(result) # prints the output of the function
# do not remove this line!

I see numerous issues with this. My comments in green in your code...

I'm putting my code in spoiler tags, in case the comments are unclear and you're frustrated...

 

 


class County:
    def __init__(self, name,population,voters):
        self.name=name
        self.population=population
        self.voters=voters
        self.turnout=voters/population
        
def highest_turnout(data):
    highest_turnout = data[0]
    for County in data:
        if (County.turnout) > (highest_turnout.turnout):
            highest_turnout = County
    return (highest_turnout.name, highest_turnout.turnout)

 

Link to comment
Share on other sites

  • Subscriber
1 hour ago, nudge said:

 

I see numerous issues with this. My comments in green in your code...

I'm putting my code in spoiler tags, in case the comments are unclear and you're frustrated...

 

  Reveal hidden contents

 



class County:
    def __init__(self, name,population,voters):
        self.name=name
        self.population=population
        self.voters=voters
        self.turnout=voters/population
        
def highest_turnout(data):
    highest_turnout = data[0]
    for County in data:
        if (County.turnout) > (highest_turnout.turnout):
            highest_turnout = County
    return (highest_turnout.name, highest_turnout.turnout)

 

 

Just one comment in the spoilers

As a general rule in programming we name classes with word that start in uppercase and variables with lowercase. So, in the for loop it would be "for county in data" vs "for County in data" as when you're reading the code you see a class County and a variable county which are two very different entities.

Link to comment
Share on other sites

  • Subscriber
7 minutes ago, Mel81x said:

Just one comment in the spoilers

 

  Hide contents

 

As a general rule in programming we name classes with word that start in uppercase and variables with lowercase. So, in the for loop it would be "for county in data" vs "for County in data" as when you're reading the code you see a class County and a variable county which are two very different entities.

 

 

that returns an attribute error when trying to execute the loop though, and now I'm trying to understand why! 

:4_joy:  

Link to comment
Share on other sites

  • Subscriber

@Mel81x phew, figured it out - was just the case of missing one capital letter xD 

Also tried something else to take advantage of inbuilt max function in python. This would also be viable, no?

class County:
    def __init__(self, name, population, voters):
        self.name=name
        self.population=population
        self.voters=voters
        self.turnout=voters/population

def highest_turnout(data):
    max_county = max(data, key=lambda c: c.turnout)
    return (max_county.name, max_county.turnout)

Link to comment
Share on other sites

Still struggling with it, however, I feel like this is MUCH better, even though it is wrong. I'm going to start checking this against your work @Mel81x and @nudge

I'm getting this error message - 

Error on line 26: result = highest_turnout(data) NameError: name 'highest_turnout' is not defined

 

class County:
  def __init__(self, init_name, init_population, init_voters):
    self.name = init_name
    self.population = init_population
    self.voters = init_voters
    self.turnout = init_voters/init_population
    
  def highest_turnout(data):
    highest_turnout = data[0]
    for county in data:
        if (county.turnout) > (highest_turnout.turnout):
            highest_turnout = county
    return (highest_turnout.name, highest_turnout.turnout)
    


allegheny = County("allegheny", 1000490, 645469)
philadelphia = County("philadelphia", 1134081, 539069)
montgomery = County("montgomery", 568952, 399591)
lancaster = County("lancaster", 345367, 230278)
delaware = County("delaware", 414031, 284538)
chester = County("chester", 319919, 230823)
bucks = County("bucks", 444149, 319816)
data = [allegheny, philadelphia, montgomery, lancaster, delaware, chester, bucks]  

result = highest_turnout(data) 
print(result) 
# do not remove this line!


allegheny = County("allegheny", 1000490, 645469)
philadelphia = County("philadelphia", 1134081, 539069)
montgomery = County("montgomery", 568952, 399591)
lancaster = County("lancaster", 345367, 230278)
delaware = County("delaware", 414031, 284538)
chester = County("chester", 319919, 230823)
bucks = County("bucks", 444149, 319816)
data = [allegheny, philadelphia, montgomery, lancaster, delaware, chester, bucks]  

result = highest_turnout(data) 
print(result) 
# do not remove this line!

 

Link to comment
Share on other sites

So here is a genuine question that I can't find the answer to online. 

Quote

class County:
  def __init__(self, init_name, init_population, init_voters):
    self.name = init_name
    self.population = init_population
    self.voters = init_voters
    self.turnout = init_voters/init_population

What the hell is the purpose of this? 

I get we have a class called County, and this makes me think that we are naming each variable. However, we never come back to this so I don't understand the purpose of this right were. The class just said (I wanted all the videos again today), that this is the naming convention used, but nothing about why or what purpose it has to put "self.name = init_name", etc.  

Link to comment
Share on other sites

  • Subscriber
36 minutes ago, Eco said:

Still struggling with it, however, I feel like this is MUCH better, even though it is wrong. I'm going to start checking this against your work @Mel81x and @nudge

I'm getting this error message - 

Error on line 26: result = highest_turnout(data) NameError: name 'highest_turnout' is not defined

 

This is just indentation issue:

 

class County:
  def __init__(self, init_name, init_population, init_voters):
    self.name = init_name
    self.population = init_population
    self.voters = init_voters
    self.turnout = init_voters/init_population
    
  def highest_turnout(data): # REMOVE INDENTATION AT THE START OF THIS LINE!
    highest_turnout = data[0]
    for county in data:
        if (county.turnout) > (highest_turnout.turnout):
            highest_turnout = county
    return (highest_turnout.name, highest_turnout.turnout)
    


allegheny = County("allegheny", 1000490, 645469)
philadelphia = County("philadelphia", 1134081, 539069)
montgomery = County("montgomery", 568952, 399591)
lancaster = County("lancaster", 345367, 230278)
delaware = County("delaware", 414031, 284538)
chester = County("chester", 319919, 230823)
bucks = County("bucks", 444149, 319816)
data = [allegheny, philadelphia, montgomery, lancaster, delaware, chester, bucks]  

result = highest_turnout(data) 
print(result) 

 

Link to comment
Share on other sites

3 minutes ago, nudge said:

This is just indentation issue:

  


class County:
  def __init__(self, init_name, init_population, init_voters):
    self.name = init_name
    self.population = init_population
    self.voters = init_voters
    self.turnout = init_voters/init_population
    
  def highest_turnout(data): # REMOVE INDENTATION AT THE START OF THIS LINE!
    highest_turnout = data[0]
    for county in data:
        if (county.turnout) > (highest_turnout.turnout):
            highest_turnout = county
    return (highest_turnout.name, highest_turnout.turnout)
    


allegheny = County("allegheny", 1000490, 645469)
philadelphia = County("philadelphia", 1134081, 539069)
montgomery = County("montgomery", 568952, 399591)
lancaster = County("lancaster", 345367, 230278)
delaware = County("delaware", 414031, 284538)
chester = County("chester", 319919, 230823)
bucks = County("bucks", 444149, 319816)
data = [allegheny, philadelphia, montgomery, lancaster, delaware, chester, bucks]  

result = highest_turnout(data) 
print(result) 

 

IT WORKED!

giphy.gif

Link to comment
Share on other sites

  • Subscriber
1 hour ago, Eco said:

So here is a genuine question that I can't find the answer to online. 

What the hell is the purpose of this? 

I get we have a class called County, and this makes me think that we are naming each variable. However, we never come back to this so I don't understand the purpose of this right were. The class just said (I wanted all the videos again today), that this is the naming convention used, but nothing about why or what purpose it has to put "self.name = init_name", etc.  

The way I understand it, the __init__ method basically creates an instance of the class and then binds the attributes to it, the self parameter (or any other one, as self is just a placeholder but it's better to use it for better readability of your code) is used to refer to the current object (instance of the class). 

This explanation was the most helpful for me:

So the first thing you need to know about classes is the difference between a class and an instance of a class. A class is like a blueprint, it tells you about some thing you want to make. An instance is the thing that gets made.

For example, if you write up a blueprint for an airplane, the blueprint is like when you define a class. The actual airplanes that get made from that blueprint are like instances of a class.

Defining a class looks like this:

class Airplane:
    pass  

(Normally you would have some more code instead of pass. )

Now once you define a class you can create instances of a class like this, Airplane(). For example,

airplane1 = Airplane()
airplane2 = Airplane()

Here we created two instances of the Airplane class and put them in the variables airplane1 and airplane2. The important thing here is that you can change airplane1 without affecting airplane2. They're two separate instances.

Okay so lets say you want to make airplanes that know where they are by keeping an x and y coordinate. You could do this:

class Airplane:
    def __init__(self, x, y):
        self.x = x
        self.y = y

This will make it so that an instance of Airplane will each have their own variables x and y. You can make an instance of airplane like this:

airplane1 = Airplane(0, 0)
airplane2 = Airplane(3, 4)

Then maybe you could create a method that finds the distance between two planes:

import math

class Airplane:
    def __init__(self, x, y):
        self.x = x
        self.y = y

    def distance(self, another_plane):
        diff_x = self.x - another_plane.x
        diff_y = self.y - another_plane.y
        distance_total = math.sqrt(diff_x**2 + diff_y**2)
        return distance_total

You could add more planes and they could all have different positions and you could find the distance between any two planes by using the distance() method.

Link to comment
Share on other sites

5 hours ago, nudge said:

This is just indentation issue:

 


class County:
  def __init__(self, init_name, init_population, init_voters):
    self.name = init_name
    self.population = init_population
    self.voters = init_voters
    self.turnout = init_voters/init_population
    
  def highest_turnout(data): # REMOVE INDENTATION AT THE START OF THIS LINE!
    highest_turnout = data[0]
    for county in data:
        if (county.turnout) > (highest_turnout.turnout):
            highest_turnout = county
    return (highest_turnout.name, highest_turnout.turnout)
    


allegheny = County("allegheny", 1000490, 645469)
philadelphia = County("philadelphia", 1134081, 539069)
montgomery = County("montgomery", 568952, 399591)
lancaster = County("lancaster", 345367, 230278)
delaware = County("delaware", 414031, 284538)
chester = County("chester", 319919, 230823)
bucks = County("bucks", 444149, 319816)
data = [allegheny, philadelphia, montgomery, lancaster, delaware, chester, bucks]  

result = highest_turnout(data) 
print(result) 

 

Also, why did the indention mess this up? I understand the importance of indentions within python, but I assumed it needed to be linear with the original "def".

Link to comment
Share on other sites

  • Subscriber
22 minutes ago, Eco said:

Also, why did the indention mess this up? I understand the importance of indentions within python, but I assumed it needed to be linear with the original "def".

It actually has to be in line with the first line of code at the top which in your case is class. When the compiler sees the def at the position you have it at in some cases it will think the DEF is part of the class thats why it failed and then below that you have more definitions in line with class so it gets confused.

Don't know if you've watched Silicon Valley but this part in the show sums it up for devs when it comes to indentation.

 

Link to comment
Share on other sites

9 hours ago, Mel81x said:

It actually has to be in line with the first line of code at the top which in your case is class. When the compiler sees the def at the position you have it at in some cases it will think the DEF is part of the class thats why it failed and then below that you have more definitions in line with class so it gets confused.

Don't know if you've watched Silicon Valley but this part in the show sums it up for devs when it comes to indentation.

 

I've been told to watch the show, but have never gotten around to it. ha

Link to comment
Share on other sites

Here is my weekly project that I'm absolutely getting beaten with. 

We had to do pseudocode earlier on the course on a finding the amount of time a words, and it's synonyms are found in a set of documents. Using this pseudocode, they want us to write the code for it. 

Here is the pseudocode - 

All_words ← [Keyword]
For each Entry in Thesaurus
	If Keyword = Entry.Word
	Then
		For each Word in Entry.Synonyms
			Add Word to All_words
		Stop

For each Search_word in All_words
	Count ← 0
	For each Document in Corpus
		For each Word in Document
			If Search_word = Word
			Then Count ← Count + 1
	Output: Search_word, Count 

Here are the instructions verbatim. 

Quote

Now, to finish the computational thinking problem solving process, implement this algorithm in Python in the space below by completing the “search” function.

As you can see, the parameter to the function is the “keyword” for which to search. Your program can also access two other variables that you will need to complete the program:

Thesaurus, which is a list of Entry objects; each Entry object has a word attribute, which is a string of characters; and an attribute called synonyms, which is a list of strings

 Corpus, which is a list of lists of strings For example, the Entry class and Thesaurus variable may be defined like this:

image.thumb.png.bb410b0955ece50a4424b377dce985bb.png

Note that the first argument to the Entry constructor is the word in the thesaurus, and the second is the list of words that are its synonyms. All words consist only of lowercase letters.

 

And the Corpus variable may be defined like this:

image.thumb.png.672ea842c6b2d1522275a1b40875a25c.png

Each document is represented as a list of words, which are all lowercase letters, and the Corpus is a list containing those lists.

You can access the Thesaurus and Corpus variables in your program without having to define them; they have already been defined and initialized with words and phrases describing a person’s emotions -- happy, sad, angry, etc. -- in the setup of this activity. For your own testing purposes, you can create your own Thesaurus and Corpus and populate them with your own data, but please do so outside the function definition, and keep in mind that the correctness of your function will be determined using the Thesaurus and Corpus that we have provided.

Your “search” function should implement the algorithm described by the pseudocode above by using the Thesaurus to find the keyword’s synonyms, and then reporting the number of occurrences of the keyword and its synonyms in all documents in the Corpus.

The output of your function should be a list of tuples, in which the first element of the tuple is the word that was searched for (either the keyword or one of its synonyms) and the second is its total number of occurrences.

For instance, if the keyword was “cat” and it occurred 120 times, and its synonym was “kitty” and it occurred 84 times, the output should be: [ (“cat”, 120), (“kitty”, 84) ] Hint: You can create a tuple variable like this: result = (“cat”, 120) And then add it to a list using the list’s “append” function.

Your program will be evaluated using the inputs “happy” and “sad” for the Thesaurus and Corpus we have provided, along with other test inputs as well. Keep in mind that you can print out the Thesaurus and Corpus objects to see what is in them and to get an idea of whether your program is producing the correct output for those inputs.

So there you have it. I'm still digesting this as I'm trying to make sure I understand what corpus they are wanting us to use, as for some reason this isn't make much sense to me. Maybe it's the Sprite and Tequila from last night though. 

Link to comment
Share on other sites

  • Subscriber
17 minutes ago, Eco said:

Here is my weekly project that I'm absolutely getting beaten with. 

We had to do pseudocode earlier on the course on a finding the amount of time a words, and it's synonyms are found in a set of documents. Using this pseudocode, they want us to write the code for it. 

Here is the pseudocode - 


All_words ← [Keyword]
For each Entry in Thesaurus
	If Keyword = Entry.Word
	Then
		For each Word in Entry.Synonyms
			Add Word to All_words
		Stop

For each Search_word in All_words
	Count ← 0
	For each Document in Corpus
		For each Word in Document
			If Search_word = Word
			Then Count ← Count + 1
	Output: Search_word, Count 

Here are the instructions verbatim. 

So there you have it. I'm still digesting this as I'm trying to make sure I understand what corpus they are wanting us to use, as for some reason this isn't make much sense to me. Maybe it's the Sprite and Tequila from last night though. 

I will take a proper look after work.

I assume there's a grader which automatically tests and grades your code once you submit it? You shouldn't worry about the Thesaurus and Corpus variables at all, as according to the instruction, they have already been setup and defined for you. So you can simply use both those variables in the "search" function you're supposed to write based on the pseudocode, without having to define them yourself. Don't overthink it, that pseudocode has pretty much all information you need, you just need to transform it into an actual code with very slight modifications.

Link to comment
Share on other sites

2 minutes ago, nudge said:

I will take a proper look after work.

I assume there's a grader which automatically tests and grades your code once you submit it? You shouldn't worry about the Thesaurus and Corpus variables at all, as according to the instruction, they have already been setup and defined for you. So you can simply use both those variables in the "search" function you're supposed to write based on the pseudocode, without having to define them yourself. Don't overthink it, that pseudocode has pretty much all information you need, you just need to transform it into an actual code with very slight modifications.

I must be because what I'm seeing is not working out at all. I'm trying to basically mimic my pseudocode (which was graded at 100%), and came up with this. 

class Entry:
    def __init__(self,input_word, input_synonyms):
      self.word = input_word
      self.synonyms = input_synonyms

def search(keyword) :
  all_words = []
  for each keyword in thesaurus :
    if keyword == entry.word
    elif
      for each word in entry_synonyms :
        add word to all_words
      break
  for search_word in all_words
    count= 0
    for each document in corpus
      for each word in document
        if search_word == word
          then count = count +1
    output: search_word, count
  

   return search(input)

input = "happy"
output = search(input)
print(output)

Which isn't working. 

And yes, they have a set up so it'll grade you as pass once you run the code and it works as desired. 

Link to comment
Share on other sites

  • Subscriber
4 minutes ago, Eco said:

I must be because what I'm seeing is not working out at all. I'm trying to basically mimic my pseudocode (which was graded at 100%), and came up with this. 


class Entry:
    def __init__(self,input_word, input_synonyms):
      self.word = input_word
      self.synonyms = input_synonyms

def search(keyword) :
  all_words = []
  for each keyword in thesaurus :
    if keyword == entry.word
    elif
      for each word in entry_synonyms :
        add word to all_words
      break
  for search_word in all_words
    count= 0
    for each document in corpus
      for each word in document
        if search_word == word
          then count = count +1
    output: search_word, count
  

   return search(input)

input = "happy"
output = search(input)
print(output)

Which isn't working. 

And yes, they have a set up so it'll grade you as pass once you run the code and it works as desired. 

You are on the right path, but some parts of the code are still not real code, so it won't work :) Some quick comments:

def search(keyword) :
  all_words = []
  for each keyword in thesaurus : #REMOVE 'EACH'
    if keyword == entry.word
    elif #REMOVE THIS, AS THERE'S ONLY ONE CONDITION HERE IN YOUR FUNCTION
      for each word in entry_synonyms : #REMOVE 'EACH' 
        add word to all_words # #SHOULD USE LIST APPEND METHOD HERE; SO WHAT IT DOES IS MAKES A LIST OF THE KEYWORD + ITS SYNONYMS
      break
  for search_word in all_words
    count= 0
    for each document in corpus #REMOVE 'EACH'
      for each word in document #REMOVE 'EACH'
        if search_word == word
          then count = count +1 #REMOVE 'THEN'
    output: search_word, count #SHOULD USE LIST APPEND AGAIN TO MAKE SURE THAT THE COUNT IS PRINTED TOGETHER WITH THE KEYWORD AND ITS SYNONYMS AS A LIST WITHIN YOUR PREVIOUS LIST THAT WAS CREATED IN THE FIRST PART OF THE FUNCTION  
  

   return search(input)
Link to comment
Share on other sites

class Entry:
    def __init__(self,input_word, input_synonyms):
      self.word = input_word
      self.synonyms = input_synonyms

def search(keyword) :
  all_words = []
  for keyword in thesaurus :
    if keyword == entry.word:
      for word in entry_synonyms :
        keyword.append(word)
     
    break
  for search_word in all_words:
    count= 0
    for document in corpus:
      for word in document:
        if search_word == word:
          count = count +1
    #I have to think of what append to use here....
  

   return search(input)

input = "happy"
output = search(input)
print(output)

 

Link to comment
Share on other sites

  • Subscriber
11 minutes ago, Eco said:

class Entry:
    def __init__(self,input_word, input_synonyms):
      self.word = input_word
      self.synonyms = input_synonyms

def search(keyword) :
  all_words = []
  for keyword in thesaurus :
    if keyword == entry.word:
      for word in entry_synonyms :
        keyword.append(word)
     
    break
  for search_word in all_words:
    count= 0
    for document in corpus:
      for word in document:
        if search_word == word:
          count = count +1
    #I have to think of what append to use here....
  

   return search(input)

input = "happy"
output = search(input)
print(output)

 

THIS SUCKS, JUST LIKE 2020.
Link to comment
Share on other sites

  • Subscriber
1 minute ago, Eco said:

Did you just quote me without any additions? xD

No, they just disappeared, ffs xD Don't know what happened there, but I just wanted to add comments to those lines:

all_words = [] #don't forget to add the keyword into this list too, as otherwise its occurences won't be counted later

for keyword in thesaurus : #missed it earlier, but it should be 'for entry in thesaurus'

for word in entry_synonyms : keyword.append(word) #should append the all_words, not keyword!!!

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

  • Recently Browsing   0 members

    No registered users viewing this page.


Sign up or subscribe to remove this ad.


×
×
  • Create New...