Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Factorial sketch(python 3)
#1
I am a fairly new python student, so please excuse any errors.
I am having trouble coming up with the correct output for this sketch i got from a friend. Could you please point out what's wrong and how to correct it?
For example, if i enter Factorial(16), i get 16 as the output.
But if i enter Factorial(4) i get 7 as the output.
This is the sketch:


n=7
def factorial(n):
    if n<0:
        n*=-1
        if (n==0):
            return 0
        f= 1
        while n>1:
            f*=n
            n-=1
            return f
Reply
#2
That indentation is not the indentation your are using. If it was, factorial(16) would return None. Please make sure the indentation in your post matches that in your code. Indentation is important in Python, and I suspect that might be where the error is.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Comparing recursion and loops using two scripts (basic factorial) Drone4four 3 2,237 Oct-11-2020, 06:48 PM
Last Post: deanhystad
  Python factorial code timebrahimy 4 67,686 Sep-27-2020, 12:23 AM
Last Post: timebrahimy
  factorial, repeating Aldiyar 4 2,788 Sep-01-2020, 05:22 PM
Last Post: DPaul
  factorial using recursive function spalisetty06 12 4,050 Aug-25-2020, 03:16 PM
Last Post: spalisetty06
  minor mistake in code for factorial spalisetty06 2 1,875 Aug-22-2020, 05:00 PM
Last Post: spalisetty06
  Factorial Code is not working when the given number is very long integer Raj_Kumar 2 2,294 Mar-31-2020, 06:40 PM
Last Post: deanhystad
  Want to print each iteration of factorial program sbabu 10 4,564 Jan-09-2020, 07:25 AM
Last Post: perfringo
  Factorial leodavinci1990 8 4,418 Jul-19-2019, 10:59 PM
Last Post: leodavinci1990

Forum Jump:

User Panel Messages

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