1

I'm trying to get my results function to call my retrieve_pub_vote_summary function, which creates a tuple. I then want my results function to print the tuple.

@application.route('/results/')
def results:
    publication = "nytimes"
    retrieve_pub_vote_summary(publication)
    print(pub_tuple)

##############################################
def retrieve_pub_vote_summary(publication):
    onevotes = 1
    twovotes = 2
    pub_tuple = (onevotes, twovotes)
    print(pub_tuple)
    return pub_tuple

pub_tuple prints fine in retrieve_pub_vote_summary. But it doesn't seem to go to results. I get "NameError: name 'pub_tuple' is not defined."

codi6
  • 516
  • 1
  • 3
  • 18
  • 1
    pub_tuple = retrieve_pub_vote_summary(publication). You need to assign it to the variable. – Mace May 09 '20 at 15:37
  • Does this answer your question? [How can I store a result of a function in a variable in Python?](https://stackoverflow.com/questions/35423564/how-can-i-store-a-result-of-a-function-in-a-variable-in-python) – wwii May 09 '20 at 15:52

1 Answers1

1

You have 2 errors in the code:

def results():                                          # added ()
    publication = "nytimes"
    pub_tuple = retrieve_pub_vote_summary(publication)  # assign the result
    print(pub_tuple)                                    # now pub_tuple is defined the line above


##############################################
def retrieve_pub_vote_summary(publication):
    onevotes = 1
    twovotes = 2
    pub_tuple = (onevotes, twovotes)
    print(pub_tuple)
    return pub_tuple


results()   # call results

Now it returns

(1, 2)
(1, 2)

pub_tuple in retrieve_pub_vote_summary is a local variable inside retrieve_pub_vote_summary. That's why it couldn't be printed and you got the error.

Mace
  • 1,355
  • 8
  • 13