Home > Software engineering >  Confused about how the return works in python
Confused about how the return works in python

Time:11-30

The program runs and the function works but I am not able to see my docCountryList in the output. Can someone tell me why?

I have this code

def ViewByCountry(docID,user_selection):
    docCountryList=[]
    for x in jfile:
        if x.get('subject_doc_id') == docID:
            docCountryList.append(x['visitor_country'])
    if user_selection == '2a':
        x = []
        y = []
        #Insert countries and number of occurences in two seperate lists
        for k,v in Counter(docCountryList).items():
            x.append(k)
            y.append(v)
        plt.title('Countries of Viewers')
        plt.bar(range(len(y)), y, align='center')
        plt.xticks(range(len(y)), x, size='small')
        plt.show()
        return docCountryList

and in my main

from program import ViewByCountry

# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    docID = input("Enter required document ID: ")
    user_selection = input("Enter selection")
    ViewByCountry(docID,user_selection)

CodePudding user response:

You never print out the value of docCountryList, so try this:

print(ViewByCountry(docID,user_selection))

This will print out the value.

You can do this as well:

lst = ViewByCountry(docID,user_selection)
print(lst)

CodePudding user response:

In your main you can change to myView = ViewByCountry(docID,user_selection) and then add print(myView). This saves the list created by your function to a variable to be printed or used later.

  • Related