The 365 Data Science team is proud to invite you to our own community forum. A very well built system to support your queries, questions and give the chance to show your knowledge and help others in their path of becoming Data Science specialists.
Ask
Anybody can ask a question
Answer
Anybody can answer
Vote
The best answers are voted up and moderated by our team

Factorial

Factorial

0
Votes
1
Answer

Hello,
Could someone, please, have a look at the code and tell me why it does not execute the commands (when I type in a number, e.g. 5, it just prints it and that’s all)?
I am a beginner and after having checked all I know, I am not able to see the error.

I would appreciate.

n = input(‘Please, give a number. Type exit to leave. \n>>> ‘)

while n.lower() != ‘exit’:
while not n.isdigit():
print(‘A digit bigger than zero, please!’)
n = input(‘Here: \n>>> ‘)

fact = 1
for i in range(1, n+1):
fact = fact*i

print(f’The factorial of {n} equals {fact}.’)


 

1 Answer

0
Votes

This will work!
 
n = int(input(“Enter the number: “))
if(n>0):
         fact = 1
         for i in range(1, n+1):
                 fact = fact*i
         print(“The factorial of ” + str(n) + ” is ” + str(fact))
else:
         print(“Please Enter positive integers”)

Thank you! I will try it out.

6 months

Welcome!

6 months