# Python Factorial Code

Page 1 of 1

## 7 Replies - 21037 Views - Last Post: 21 August 2010 - 06:36 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=186437&amp;s=955c94cb3b5aee9c6fa6699a191725d0&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 alienmasoud

Reputation: -1
• Posts: 3
• Joined: 19-December 09

# Python Factorial Code

Posted 17 August 2010 - 05:48 PM

Hi
I need to know python
I don't know why this code for factorial function does not work correctly
here the code:
```deff fact(x):
while x>0:
fac=x*(x-1)
x=x-2
if x==1:
break
return fac

```

the output for fact(4) is 20 it means the while loop does not work why???!!
and I read that with python I can write codes better and easier in c and c++ and boost them,
and embed codes...this makes me to learn python.!!! is it true!!!???

This post has been edited by macosxnerd101: 17 August 2010 - 05:51 PM

Is This A Good Question/Topic? -1

## Replies To: Python Factorial Code

### #2 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 11237
• Posts: 42,286
• Joined: 27-December 08

## Re: Python Factorial Code

Posted 17 August 2010 - 05:51 PM

Moved to Python.

### #3 guahguahmonster

Reputation: 68
• Posts: 209
• Joined: 29-August 07

## Re: Python Factorial Code

Posted 18 August 2010 - 12:05 AM

Hey, wrong forum?

alienmasoud, on 17 August 2010 - 04:48 PM, said:

Hi
I need to know python
I don't know why this code for factorial function does not work correctly
here the code:
```deff fact(x):
while x>0:
fac=x*(x-1)
x=x-2
if x==1:
break
return fac

```

the output for fact(4) is 20 it means the while loop does not work why???!!

When thinking about code, it's best to figure out what the code is doing at each step.

So, wehn you run fact(4), in the first iteration of the while loop, x = 4, so fac = 4 * 3 = 12. Okay. So now x = x - 2, so now x = 2, and the while loop can run again. So now fac = 2 * 1 = 2. Uh oh! You totally lost the value of your previous calculations; you overwrote it with this new calulation. So what happens next? Now x = x - 2, so x = 0. And now the while loop ends, and the current value of fac is returned, and that value is 2.

So you need to figure out how to change the code so that you don't lose your previous calculations!

alienmasoud, on 17 August 2010 - 04:48 PM, said:

and I read that with python I can write codes better and easier in c and c++ and boost them,
and embed codes...this makes me to learn python.!!! is it true!!!???

I'm not sure what you mean by "boost"ing your code. If anything, boost is a C++ library...

As for "embedding" code... there are several things this could mean. You can call C functions from Python, or you can call Python functions from C, which might be counted as "embedding" one language inside another. In addition, since Python is considered a scripting language, you can "embed" it inside any application you write to allow the user to write scripts for your application. Again, I'm not sure exactly what you mean here.

Writing code "better and easier" is a subjective thing. What does "better" mean? Better could mean it runs faster (this is probably not true for Python), is easier to read (possibly true for Python), is easier to maintain (probably true as well), or a whole host of other metrics.

C and C++ are very low-level languages that give you control of the machine, allowing you to work with pointers directly and also making you manage your own memory. Python is by comparison a higher level language, and it takes care of memory management for you, along with other things (no curly braces, nice clean syntax, enforced indentation etc.) that might make writing code easier, and it also tends to make maintaining your code in the future easier.

In the end, you must choose the right tool for the job! It would be a great advantage to use Python for code where you don't need blazing fast performance and a faster development cycle offered by a scripting language is helpful, but obviously you would not want to use Python for programming, say, a microcontroller on an embedded device...

This post has been edited by guahguahmonster: 18 August 2010 - 12:08 AM

Reputation:

## Re: Python Factorial Code

Posted 18 August 2010 - 12:55 AM

```>>> def fact(n):
...   if n > 1:
...     return n * fact(n - 1)
...   return 1
...
>>> fact(3)
6
>>>
>>> def fact_loop(n):
...   res = 1
...   while n > 1:
...     res *= n
...     n -= 1
...   return res
...
>>> fact_loop(3)
6
>>>

```

### #5 alienmasoud

Reputation: -1
• Posts: 3
• Joined: 19-December 09

## Re: Python Factorial Code

Posted 21 August 2010 - 04:22 AM

c.user, on 17 August 2010 - 11:55 PM, said:

```>>> def fact(n):
...   if n > 1:
...     return n * fact(n - 1)
...   return 1
...
>>> fact(3)
6
>>>
>>> def fact_loop(n):
...   res = 1
...   while n > 1:
...     res *= n
...     n -= 1
...   return res
...
>>> fact_loop(3)
6
>>>

```

unfortunately the second code you wrote didn't work right, you know I wanna know why I write factorial function code in so many ways that works in c, ... and I'm sure the code style in second code u wrote is wrong but I don't know why, I have python 2.7 and this code works also
```>>>def fact(n)
...f=1
...while n>1:
...f=f*n;

...n=n-1
...return f
>>>fact(5)
120

```

I don't know why
but I need to know why if I didn't write (;)after f=f*n that code didn't work?!!! and I didn't see anything like (;)in python before to do like this!!!!!!!!!!!!!

This post has been edited by alienmasoud: 21 August 2010 - 04:26 AM

### #6 alienmasoud

Reputation: -1
• Posts: 3
• Joined: 19-December 09

## Re: Python Factorial Code

Posted 21 August 2010 - 04:34 AM

guahguahmonster, on 17 August 2010 - 11:05 PM, said:

Hey, wrong forum?

alienmasoud, on 17 August 2010 - 04:48 PM, said:

Hi
I need to know python
I don't know why this code for factorial function does not work correctly
here the code:
```deff fact(x):
while x>0:
fac=x*(x-1)
x=x-2
if x==1:
break
return fac

```

the output for fact(4) is 20 it means the while loop does not work why???!!

When thinking about code, it's best to figure out what the code is doing at each step.

So, wehn you run fact(4), in the first iteration of the while loop, x = 4, so fac = 4 * 3 = 12. Okay. So now x = x - 2, so now x = 2, and the while loop can run again. So now fac = 2 * 1 = 2. Uh oh! You totally lost the value of your previous calculations; you overwrote it with this new calulation. So what happens next? Now x = x - 2, so x = 0. And now the while loop ends, and the current value of fac is returned, and that value is 2.

So you need to figure out how to change the code so that you don't lose your previous calculations!

alienmasoud, on 17 August 2010 - 04:48 PM, said:

and I read that with python I can write codes better and easier in c and c++ and boost them,
and embed codes...this makes me to learn python.!!! is it true!!!???

I'm not sure what you mean by "boost"ing your code. If anything, boost is a C++ library...

As for "embedding" code... there are several things this could mean. You can call C functions from Python, or you can call Python functions from C, which might be counted as "embedding" one language inside another. In addition, since Python is considered a scripting language, you can "embed" it inside any application you write to allow the user to write scripts for your application. Again, I'm not sure exactly what you mean here.

Writing code "better and easier" is a subjective thing. What does "better" mean? Better could mean it runs faster (this is probably not true for Python), is easier to read (possibly true for Python), is easier to maintain (probably true as well), or a whole host of other metrics.

C and C++ are very low-level languages that give you control of the machine, allowing you to work with pointers directly and also making you manage your own memory. Python is by comparison a higher level language, and it takes care of memory management for you, along with other things (no curly braces, nice clean syntax, enforced indentation etc.) that might make writing code easier, and it also tends to make maintaining your code in the future easier.

In the end, you must choose the right tool for the job! It would be a great advantage to use Python for code where you don't need blazing fast performance and a faster development cycle offered by a scripting language is helpful, but obviously you would not want to use Python for programming, say, a microcontroller on an embedded device...

Tnx a lot!!!!!!!! u know I've heard Python is very good(not sure)and easy to use(sure) for workin' with web pages and ... but i need more info about (I would like use python codes instead c++ or... inside c or...)that if u would help me I'll appreciate it, thanks so much again!!!!!!!

This post has been edited by alienmasoud: 21 August 2010 - 04:38 AM

### #7 moopet

• binary decision maker

Reputation: 339
• Posts: 1,185
• Joined: 02-April 09

## Re: Python Factorial Code

Posted 21 August 2010 - 04:35 AM

alienmasoud, on 21 August 2010 - 10:22 AM, said:

I don't know why
but I need to know why if I didn't write (;)after f=f*n that code didn't work?!!! and I didn't see anything like (;)in python before to do like this!!!!!!!!!!!!!

The only reason that would be needed is if you were writing the two lines inside the while loop as if they were one line. It's hard to tell because your pasted code has no indentation whatsoever but I suspect you just typed this into the python shell without indenting anything. Paste it into a file with proper indentation and it'll work.

By the way, adding more punctuation does not make your question any clearer.

Reputation:

## Re: Python Factorial Code

Posted 21 August 2010 - 06:36 PM

try this file also

#### Attached File(s)

•  pyfact.txt (166bytes)