1

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)
HIMANSHU PANDEY
  • 684
  • 1
  • 10
  • 22
TooMuch
  • 21
  • 2
  • Return the value of the dictionary from the first function and then pass it to the next. – Reti43 Oct 18 '21 at 10:48
  • Sorry to pester you more, do you have any resources that would show me how to do this? – TooMuch Oct 18 '21 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)`. – FObersteiner Oct 18 '21 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](https://stackoverflow.com/q/1369526/10197418). – FObersteiner Oct 18 '21 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()`. – Reti43 Oct 18 '21 at 10:58
  • @MrFuppes as stupid as this question is I must ask it, please explain why it wouldn't still make it a global variable though? – TooMuch Oct 18 '21 at 10:59

2 Answers2

1

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.

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)

HIMANSHU PANDEY
  • 684
  • 1
  • 10
  • 22