aaaa
aaaa

Reputation: 265

How do I return the product of a while loop

I don't get the concept of loops yet. I got the following code:

x=0
while  x < n:
   x = x+1
   print x

which prints 1,2,3,4,5.

That's fine, but how do I access the computation, that was done in the loop? e.g., how do I return the product of the loop( 5*4*3*2*1)?

Thanks.

Edit:

That was my final code:

def factorial(n):
   result = 1
   while n >= 1:
      result = result *n
      n=n-1
   return result

Upvotes: 2

Views: 4064

Answers (7)

rumpelsepp
rumpelsepp

Reputation: 103

use a for loop:

sum_ = 1
for i in range(1, 6):
    sum_ *= i
print sum_

Upvotes: 2

user2489252
user2489252

Reputation:

If you prefer to keep your while loop structure, you could do it like (there are 1000 +1 ways to do it ...):

x=1
result = 1
while  x <= n:
   x += 1
   result *= x

Where result will store the factorial. You can then just return or print out result, or whatever you want to do with it.

Upvotes: 1

Slater Victoroff
Slater Victoroff

Reputation: 21914

Alternately you could use the yield keyword which will return the value from within the while loop. For instance:

def yeild_example():
    current_answer = 1
    for i in range(1,n+1):
        current_answer *= i
        yield current_answer

Which will lazily evaluate the answers for you. If you just want everything once this is probably the way to go, but if you know you want to store things then you should probably use return as in other answers, but this is nice for a lot of other applications.

This is called a generator function with the idea behind it being that it is a function that will "generate" answers when asked. In contrast to a standard function that will generate everything at once, this allows you to only perform calculations when you need to and will generally be more memory efficient, though performance is best evaluated on a case-by-case basis. As always.

**Edit: So this is not quite the question OP is asking, but I think it would be a good introduction into some of the really neat and flexible things about python.

Upvotes: 2

Alex Kulinkovich
Alex Kulinkovich

Reputation: 4768

to access the computation done in the loop, you must use counter(with useful and understandable name), where you will store the result of computation. After computation you just return or use the counter as the product of the loop.

sum_counter=0
x=0
while  x < 10:
   sum_counter +=x
   x+=1
print sum_counter

Upvotes: 0

Vladimir
Vladimir

Reputation: 10493

You want to introduce one more variable (total) which contains accumulated value of a bunch of actions:

total = 1
x = 1
while x <= 5:
   total *= x
   x += 1
   print x, total
print 'total:', total

Actually, more pythonic way:

total = 1
n = 5
for x in xrange(1, n + 1):
    total *= x
print total

Note, that the initial value of total must be 1 and not 0 since in the latter case you will always receive 0 as a result (0*1*.. is always equals to 0).

Upvotes: 3

John
John

Reputation: 13699

A "one-liner"

>>> import operator
>>> reduce(operator.mul, xrange(1, n + 1))
120
>>> 

Upvotes: 3

Martijn Pieters
Martijn Pieters

Reputation: 1121266

By storing that product and returning that result:

def calculate_product(n):
    product = 1
    for x in range(n):
        product *= x + 1

    return product

Now we have a function that produces your calculation, and it returns the result:

print calculate_product(5)

Upvotes: 3

Related Questions