Having problems passing info from one function to another (Python)

NutMan

I'm fairly new to python (been taking classes for a few months) and I've come across a reoccurring problem with my code involving passing information, such as an integer, from one function to another. In this case, I'm having problems with passing "totalPints" from "def getTotal" to "def averagePints" (totalPints in def averagePints defaults to 0).

def main():
    endQuery = "n"

    while endQuery == "n":
        pints = [0] * 7
        totalPints = 0
        avgPints = 0
        option = ""

        print("Welcome to the American Red Cross blood drive database.")
        print()
        print("Please enter 'w' to write new data to the file. Enter 'r' to read data currently on file. Enter 'e' to "
              "end the program.")

        try:
            option = input("Enter w/r/e: ")
        except ValueError:
            print()
            print("Please input only w/r/e")

        if option == "w":
            print()

            def getPints(pints):
                index = 0
                while index < 7:
                    pints[index] = input("Input number of Pints of Blood donated for day " + str(index + 1) + ": ")
                    print(pints)
                    index = index + 1

            getPints(pints)

            def getTotal(pints, totalPints):
                index = 0
                while index < 7:
                    totalPints = totalPints + int(pints[index])
                    index = index + 1
                print(totalPints)

            getTotal(pints, totalPints)

            def averagePints(totalPints, avgPints):
                avgPints = float(totalPints) / 7
                print(avgPints)

            averagePints(totalPints, avgPints)

Passing information from "def getPints" to "def getTotal" works fine, and both print the accurate information, but nothing passes from "def getTotal" to "def averagePints" and returns 0. What am I doing wrong in this case? Is it something to do with the scope of the variables listed above?

This is my first time posting to Stack Overflow because I could find any fixes to what I am having troubles with. What I expect to happen from this code is passing the number from "totalPints" in "def getTotal" to "def averagePints" to make a calculation with that number. I tried messing with the scopes of the declared variables and the order of when functions are called but I still can't really tell what I'm missing. All I know is that the value of "totalPints" in "def averagePints" always returns 0.

Stephan Pieterse

You have a variable scoping issue. In getTotal, totalPints is being updated with its value local to the function, not the global one like you are expecting. Returning the new value from the function and assigning it seems to have the intended effect. Below is the updated snippet:

            def getTotal(pints, totalPints):
                index = 0
                while index < 7:
                    totalPints = totalPints + int(pints[index])
                    index = index + 1
                print(totalPints)
                return totalPints

            totalPints = getTotal(pints, totalPints)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Python and Tkinter, carry list info from one function to another

Python: Passing an object instance from one function to another?

Passing returned value from one function to another with Python

Passing values from one function to another function

Passing a variable from one function to another function

use info from one function to another

Multiple Intents passing listView info from one activity to another

Passing boolean info from one class to another in Java

Having trouble passing values from one class to another

Trouble passing links from one function to another

Passing data from one function to another in React

Passing variable value from one function to another

Passing a range from one VB function to another

Passing lists from one function to another in Swift

Having some problems trying to call a function from another script

Passing two values from one Matlab function to another in one line

Passing a value from one function to another function in React.js

Passing function in React.js from one function component to another

Passing a Variable from one JS function to another function

Passing value from one function to another function in C

Having problems with function definition in python

Polymer JS passing function from one component to another?

Passing value from one function to another C++

Passing values from one function to another with React navigation 5

Passing local variable from one function to another in JavaScript?

Passing string parameters into function from one component to another in react

Passing in variable number of args from one function to another in Swift

Passing Input Parameters from one Step Function to another

Passing variable value from one function to another in autocomplete with AJAX call