Python Forum
minor mistake in code for factorial
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
minor mistake in code for factorial
#1
Hello,
I believe, I have written it correctly, but I am missing something.
'''write a program to get the factorial of a given number'''
def fact(number):
    result = 1
    while number > 1:
        result = result * number
        number = number -1
        return result
print(fact(5))
Output:
5
Reply
#2
Remember that when a return statement is encountered, the function immediately returns. So, only one iteration of your while loop is performed, because the function returns at the end of that.
Reply
#3
(Aug-22-2020, 04:51 PM)ndc85430 Wrote: Remember that when a return statement is encountered, the function immediately returns. So, only one iteration of your while loop is performed, because the function returns at the end of that.

I am so stupid after all, Thank you so much
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Solved] Please, help me find a simple mistake AlekseyPython 2 1,756 Jun-17-2021, 12:20 PM
Last Post: AlekseyPython
  [split] Could you please clarify where i did mistake also how run without admin right Abubakkar 1 1,797 Jun-14-2021, 09:32 AM
Last Post: Larz60+
  Please help to me to find my mistake in code leonardin 2 1,849 Nov-29-2020, 04:17 PM
Last Post: Larz60+
  Comparing recursion and loops using two scripts (basic factorial) Drone4four 3 2,266 Oct-11-2020, 06:48 PM
Last Post: deanhystad
  Python factorial code timebrahimy 4 69,716 Sep-27-2020, 12:23 AM
Last Post: timebrahimy
  factorial, repeating Aldiyar 4 2,814 Sep-01-2020, 05:22 PM
Last Post: DPaul
  factorial using recursive function spalisetty06 12 4,102 Aug-25-2020, 03:16 PM
Last Post: spalisetty06
  Simple mistake about for Nomatter 4 2,261 Jul-16-2020, 02:24 PM
Last Post: Nomatter
  Factorial Code is not working when the given number is very long integer Raj_Kumar 2 2,313 Mar-31-2020, 06:40 PM
Last Post: deanhystad
  Install Mistake jlerette5 1 1,903 Feb-18-2020, 12:19 AM
Last Post: jefsummers

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020