Home > Mobile >  How to pass one varaible/dictionary between two functions in Python
How to pass one varaible/dictionary between two functions in Python

Time:10-19

I'm trying to pass a dictionary between two functions, I don't want there to be a global variable how would one allow for this to happen?

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)

CodePudding user response:

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)

CodePudding user response:

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.

  • Related