Jump to content

How do I use a variable from another function Python

I've got two methods, a method which takes a link and processes it, and then another which does the main methods.

 

Within the main, I want to grab the name of one of the variables within the first function, how do I go about this? This is my code:

 

import requests
import nltk
from bs4 import BeautifulSoup
import string
from nltk.corpus import stopwords
from collections import Counter


def linkInput():
    # input which will allow the end user to input a link or file location
    linkInput = input ("Enter the website address: ")
    # processes the link using the requests module (linkText variable) and then processes linkText using the .text method (savedLinkText)
    linkText = requests.get(linkInput)
    print ("The output of the HTML document looks like this: ")
    savedLinkText = linkText.text
    print(" ")

def mainProcesing():
    linkInput()
    #prints the text of the link through the savedLinkText variable.  
    #grabs the savedLinkText variable from the linkInput() method.
    print(savedLinkText)
    #text in savedLinkText is then parsed through the BeautifulSoup html parser saved as the variable linkParsed.
    linkParsed = BeautifulSoup(savedLinkText, 'html.parser')

    #the parsed link then gets scraped for its text, without the tags in the variable parsedText
    parsedText = linkParsed.get_text()

    #extra spaces are then removed, joining the parsedText variable contents with split.
    strippedText = " ".join(parsedText.split())

    print(strippedText)

    #the strippedText contents are then tokenised using the natural toolkit language module, via its tokenize method.
    tokenList = nltk.word_tokenize(strippedText)

    print(" ")
    print(" ")
    print(tokenList)

    #tokenised list (tokenList) is then stripped of any punctuation values such as ' , . ? or ! which may be seperate from any text values (tokenListNoPunctuation).  This list is then normalised (tokenListNormalised)
    tokenListNoPunctuation = [''.join(c for c in s if c not in string.punctuation) for s in tokenList]
    tokenListNormalised = [s for s in tokenListNoPunctuation if s]
    print(" ")


    print(len(tokenListNormalised))

    #stop words are then removed which are part of the english words list in stopwords.words from the nltk module in a variable named stopwordsRemoved.
    stopwordsRemoved = [word for word in tokenListNormalised if word not in stopwords.words('english')]
    print(" ")
    print(" ")

    #now the stop words are removed, the text in variable stopwordsRemoved is then tagged using the pos_tag method through the nltk module, in a variable named taggedText
    taggedText = nltk.pos_tag(stopwordsRemoved)

    print(len(taggedText))

    #the following four variables (removedSingularNouns,removedProperNouns,removedProperSingularNouns,removedAllNouns) are then used to remove nouns (singular noun, proper noun, proper singular noun and proper plural nouns)
    #this is done in four stages, with the final list being the 'removedAllNouns'variable
    removedSingularNouns = [s for s in taggedText if s[1] != 'NN'] 
    removedProperNouns = [s for s in removedSingularNouns if s[1] != 'NNP']
    removedProperSingularNouns = [s for s in removedProperNouns if s[1] != 'NNS']
    removedAllNouns = [s for s in removedProperSingularNouns if s[1] != 'NNPS']
    
    print(len(removedSingularNouns))
    print(len(removedProperNouns))
    print(len(removedProperSingularNouns))
    print(len(removedAllNouns))

    #counter named wordCounter is then used using the collections module which associates a counter for each of the tagged values, and lists them from most used to least used.
    wordCounter = Counter(removedAllNouns)
    print(len(wordCounter))
    print(wordCounter)

 

Link to comment
Share on other sites

Link to post
Share on other sites

I'm by no means a Python expert, but is it an option to move them out into a global variable?

System/Server Administrator - Networking - Storage - Virtualization - Scripting - Applications

Link to comment
Share on other sites

Link to post
Share on other sites

8 minutes ago, Eniqmatic said:

I'm by no means a Python expert, but is it an option to move them out into a global variable?

You can do that, but he could also return whatever he wants to use. 

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, djdwosk97 said:

You can do that, but he could also return whatever he wants to use. 

How would I go about this? The variable which I would like to use in the mainProcessing function from the linkInput function is named 'savedLinkText'.  I've not got a huge amount of experience with Python!

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, Danboy360 said:

How would I go about this? The variable which I would like to use in the mainProcessing function from the linkInput function is named 'savedLinkText'.  I've not got a huge amount of experience with Python!

at the end of the linkInput(), put

return saveLinkText

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to comment
Share on other sites

Link to post
Share on other sites

#!/usr/bin/python

# Function definition is here
def changeme( mylist ):
   "This changes a passed list into this function"
   mylist.append([1,2,3,4]);
   print "Values inside the function: ", mylist
   return

# Now you can call changeme function
mylist = [10,20,30];
changeme( mylist );
print "Values outside the function: ", mylist

You can return values like this, by reference. 

Link to comment
Share on other sites

Link to post
Share on other sites

4 minutes ago, djdwosk97 said:

You can do that, but he could also return whatever he wants to use. 

Yeah returning is an option, but the way he worded/I interpreted it made it sound like he didn't want to do that initially, but from the response I guess he does!

System/Server Administrator - Networking - Storage - Virtualization - Scripting - Applications

Link to comment
Share on other sites

Link to post
Share on other sites

I've done this:

 

def linkInput():
    # input which will allow the end user to input a link or file location
    linkInput = input ("Enter the website address: ")
    # processes the link using the requests module (linkText variable) and then processes linkText using the .text method (savedLinkText)
    linkText = requests.get(linkInput)
    print ("The output of the HTML document looks like this: ")
    savedLinkText = linkText.text
    print(" ")
    return savedLinkText
    
def mainProcessing():
    linkInput()
    #prints the text of the link through the savedLinkText variable.  
    #grabs the savedLinkText variable from the linkInput() method.
    print(savedLinkText)
    #text in savedLinkText is then parsed through the BeautifulSoup html parser saved as the variable linkParsed.
    linkParsed = BeautifulSoup(savedLinkText, 'html.parser')

 

However I'm getting the error: 

 

line 25, in mainProcessing
    print(savedLinkText)
NameError: name 'savedLinkText' is not defined
>>> 

 

Link to comment
Share on other sites

Link to post
Share on other sites

5 minutes ago, Danboy360 said:

I've done this:

 


def linkInput():
    # input which will allow the end user to input a link or file location
    linkInput = input ("Enter the website address: ")
    # processes the link using the requests module (linkText variable) and then processes linkText using the .text method (savedLinkText)
    linkText = requests.get(linkInput)
    print ("The output of the HTML document looks like this: ")
    savedLinkText = linkText.text
    print(" ")
    return savedLinkText
    
def mainProcessing():
    linkInput()
    #prints the text of the link through the savedLinkText variable.  
    #grabs the savedLinkText variable from the linkInput() method.
    print(savedLinkText)
    #text in savedLinkText is then parsed through the BeautifulSoup html parser saved as the variable linkParsed.
    linkParsed = BeautifulSoup(savedLinkText, 'html.parser')

 

However I'm getting the error: 

 


line 25, in mainProcessing
    print(savedLinkText)
NameError: name 'savedLinkText' is not defined
>>> 

 

 

Change the first line of mainProcessing to: 

savedLinkText = linkInput()

PSU Tier List | CoC

Gaming Build | FreeNAS Server

Spoiler

i5-4690k || Seidon 240m || GTX780 ACX || MSI Z97s SLI Plus || 8GB 2400mhz || 250GB 840 Evo || 1TB WD Blue || H440 (Black/Blue) || Windows 10 Pro || Dell P2414H & BenQ XL2411Z || Ducky Shine Mini || Logitech G502 Proteus Core

Spoiler

FreeNAS 9.3 - Stable || Xeon E3 1230v2 || Supermicro X9SCM-F || 32GB Crucial ECC DDR3 || 3x4TB WD Red (JBOD) || SYBA SI-PEX40064 sata controller || Corsair CX500m || NZXT Source 210.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×