Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

What is wrong with my code, Please check my code?

a = int(input())
if (a==0 or a==1 or a<1):
  print("factorial is one") 
else:
  fact =1
  while(a>1):
   fact = fact *a
   a = a-1
   print("factorial:",fact)

and Output is Like:
6
factorial: 6
factorial: 30
factorial: 120
factorial: 360
factorial: 720

but I want only 720

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Indentation problem, make print outside while

a = int(input())
if (a==0 or a==1 or a<1):
  print("factorial is one") 
else:
  fact =1
  while(a>1):
   fact = fact *a
   a = a-1
  print("factorial:",fact)

N.B:
the best way to do a factorial, is use a recursive function

def fact(n):
  if n == 1:
    return 1
  else:
    return n * fact(n-1) 


number = int(input('Your nuumber'))

print('factorial', fact(number))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading