2

I want to pass a dictionary between two functions, but how to do this without using a global variable?

I'm trying to pass the dictionary that is in my "fileProcessing" function into the "swappingKandV_PrintingResults" function without having a global variable being modified.

   dictionary = dict()


fileinputname = input("Please Input File Name: ")
try:
    filehandling = open(fileinputname)
except:
     print("Invalid Entry")
     quit()

rawfile = filehandling.readlines()

def fileProcessing(rawfile):
    for iteration in(range(len(rawfile))):
        rawfile[iteration] = rawfile[iteration].lower()
    for line in rawfile:
        line.rstrip()
        line.split()
        for words in line:
            letter = words.split()
            for iteration in letter:
                if iteration.isalpha() :
                    dictionary[iteration] = dictionary.get(iteration, 0) + 1

def swappingKandV_PrintingResults(dictionary):
    finalresults = []
    for (k,v) in dictionary.items():
        newtuple = (v, k)
        finalresults.append(newtuple)
    finalresults = sorted(finalresults, reverse=True)
    for iteration in finalresults:
        print(iteration)

fileProcessing(rawfile)
swappingKandV_PrintingResults(dictionary)
6
  • Return the value of the dictionary from the first function and then pass it to the next. Commented Oct 18, 2021 at 10:48
  • Sorry to pester you more, do you have any resources that would show me how to do this? Commented Oct 18, 2021 at 10:51
  • just add return dictionary to the end of fileProcessing function. Then assign the returned value to a variable; e.g. d = fileProcessing(rawfile). Now you can call swappingKandV_PrintingResults(d). Commented Oct 18, 2021 at 10:53
  • as a side note, you should use Python's with when opening file (so that the file pointer gets closed correctly etc.). See e.g. here. Commented Oct 18, 2021 at 10:55
  • Irrelevant, but line.rstrip() doesn't modify the original object, but creates a new string which you have to save yourself, i.e., line = line.rstrip(). Same for line.split(). Commented Oct 18, 2021 at 10:58

2 Answers 2

2

By making the first function create and return the dictionary. Then pass that returned dictionary to the second function.

fileinputname = input("Please Input File Name: ")
try:
    filehandling = open(fileinputname)
except:
     print("Invalid Entry")
     quit()

rawfile = filehandling.readlines()

def fileProcessing(rawfile):
    dictionary = {}
    for iteration in(range(len(rawfile))):
        rawfile[iteration] = rawfile[iteration].lower()
    for line in rawfile:
        line.rstrip()
        line.split()
        for words in line:
            letter = words.split()
            for iteration in letter:
                if iteration.isalpha() :
                    dictionary[iteration] = dictionary.get(iteration, 0) + 1
    return dictionary

def swappingKandV_PrintingResults(dictionary):
    finalresults = []
    for (k,v) in dictionary.items():
        newtuple = (v, k)
        finalresults.append(newtuple)
    finalresults = sorted(finalresults, reverse=True)
    for iteration in finalresults:
        print(iteration)

swappingKandV_PrintingResults(fileProcessing(rawfile))

From the way you phrased the question, it seems you have some confusion on how to work with passing arguments to functions and how to handle scope. I would suggest having at look at what a variable is in Python to begin with and then what passing it to a function means.

Sign up to request clarification or add additional context in comments.

Comments

0

You can accomplish this task in 2 ways:

1. Nested Function Call :
If you want to necessarily call 2nd function after 1st, just write - 'swappingKandV_PrintingResults(dictionary)' as the ending line in the fileProcessing function.

2. Accepting Return from 1st and Passing as Argument to 2nd :
As insisted by @Reti43 too, just write - 'return dictionary' as the ending line in the fileProcessing function and replace your last 2 lines of code by -
Dict = fileProcessing(rawfile)
swappingKandV_PrintingResults(Dict)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.