Programmer XYZ
Programmer XYZ

Reputation: 408

Returning multiple integers as separate variables

I am trying to make a program that grabs 5 integers from the user, and then finds the average of them. I have it set up to take in the 5 numbers, but how do I return them all as separate variables so I can use them later on? Thanks!

def main():
    x = 0
    testScoreNumber = 1
    while x < 5:
        getNumber_0_100(testScoreNumber)
        x += 1
        testScoreNumber += 1

    calcAverage(score1, score2, score3, score4, score5)

    print(calculatedAverage)

def getNumber_0_100(testnumber):
    test = int(input("Enter test score " + str(testnumber) + ":"))
    testcount = 0
    while testcount < 1:
        test = int(input("Enter test score " + str(testnumber) + ":"))

        if test > 0 or test < 100:
            testcount += 1

    return test

^Here is the problem, the everytime this function runs, I want it to return a different value to a different variable. Ex. test1, test2, test3.

def calcAverage(_score1,_score2,_score3,_score4,_score5):
    total = _score1 + _score2 + _score3 + _score4 + _score5
    calculatedAverage = total/5

    return calculatedAverage

Upvotes: 0

Views: 258

Answers (3)

mgilson
mgilson

Reputation: 310049

You need to store the result somewhere. It is usually (always?) a bad idea to dynamically create variable names (although it is possible using globals). The typical place to store the results is in a list or a dictionary -- in this case, I'd use a list.

change this portion of the code:

x = 0
testScoreNumber = 1
while x < 5:
    getNumber_0_100(testScoreNumber)
    x += 1
    testScoreNumber += 1

to:

results = []
for x in range(5):
    results.append( getNumber_0_100(x+1) )

which can be condensed even further:

results = [ getNumber_0_100(x+1) for x in range(5) ]

You can then pass that results list to your next function:

avg = get_ave(results[0],results[1],...)
print(avg)

Or, you can use the unpacking operator for shorthand:

avg = get_ave(*results)
print(avg)

Upvotes: 3

kevintodisco
kevintodisco

Reputation: 5191

Python allows you to return a tuple, and you can unroll this tuple when you receive the return values. For example:

def return_multiple():
    # do something to calculate test1, test2, and test3
    return (test1, test2, test3)

val1, val2, val3 = return_multiple()

The limitation here though is that you need to know how many variables you're returning. If the number of inputs is variable, you're better off using lists.

Upvotes: 0

vicvicvic
vicvicvic

Reputation: 6244

It isn't the responsibility of the returning function to say what the caller does with its return value. In your case, it would be simple to let main have a list where it adds the return values. You could do this:

scores = []
for i in range(5):
    scores.append(getNumber_0_100(i))

calcAverage(*scores)

Note that *scores is to pass a list as arguments to your calcAverage function. It's probably better to have calculateAverage be a general function which takes a list of values and calculates their average (i.e. doesn't just work on five numbers):

def calcAverage(numbers):
    return sum(numbers) / len(numbers)

Then you'd call it with just calcAverage(scores)

A more Pythonic way to write the first part might be scores = [getNumber_0_100(i) for i in range(5)]

Upvotes: 1

Related Questions