i need to build a recursive function which calculates the average of specific numbers entered ... so its fine till now but ... it says the number of entries is determined by the user's interest .. so i got some ideas i kinda stopped here

i made a function which would ask the user if he wants to add more numbers after adding 2 numbers =

def ify(x):
    if (x == yes):
        print "enter the number"
        c = input()
    else:
        q = a + b/2

then i thought this would do

from wow import ify
print "Enter two number"
a = input()
b = input()
print "would you like to enter more numbers?"
x = raw_input()
y = ify(x)
print "would you like to enter more numbers?"
x = raw_input()
y = ify(x)

but it didnt ....
and i got lost .. i dont know where the recusion should happen
i also had an idea in the calculation like when adding the numbers then divide by each number eg.
a + b + c / a-(a-1) + b-(b-1) + c-(c-1)

ny ideas ?

Recommended Answers

All 2 Replies

I'm not quite sure what you're doing, but my guess is that you're trying to make a program that will, for example, find the average of 3+4+5 and then later could have 6 added into it and also averaged correctly.

If I'm understanding this right then what you have to do is find a way to remember how many numbers were originally averaged together. So 3+4+5 averages out to be 4, you would also need the data that states this is composed of three numbers. When adding a new number to this average it would take the number of numbers (3) and multiply it by the average (4), then add in the six and divide by the new number of numbers (4) to get 4.5.

Hope that helps.

Here is example how you could approach this project:

# exploring recursion

def average(qq=[]):
    """
    using empty list as default will make the list static
    recursion forms its own loop, break out with return
    """
    try:
        x = float(raw_input("Enter number (q to quit): "))
        qq.append(x)
        print "data list =", qq
        avg = sum(qq)/len(qq)
        print "average   =", avg
        # the function calls itself (recursion)
        average()
    except:
        # actually anything but number will quit
        print 'quit'
        return


average()

Not perfect, but start.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.