Python Forum

Full Version: permutations calculation with a while instruction
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

I wrote this code

x=input('donnez un nombre')
y=int(x)
d=y
while d>=1:
	y=y*(y-1)
	d=d-1
print(y)
but for 4 in input cmd give me 298995972 instead of 24..

why?

Best regards,
You can debug your code by adding print('Current state:', y, y-1) after while loop declaration.
If you do that, you will see the output:

Output:
4 3 12 11 132 131 17292 17291 298995972 298995971
Tip: the counter, variable d, does not affect on variable y that accumulates the result. So, you need to do some changes in line 5 and probably line 3 of your code.
Thank you,

Here a code which seem ok

x=input('donnez un nombre')
y=int(x)
a=1
while y>=1:
	a=a*y
	print('Current state:', y, y-1)
	y=y-1
print(a)