Python Forum
HELP!plz! How can i combine this two function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
HELP!plz! How can i combine this two function
#1
Star 
def getFacNumList(n):
    a=1
    index=2
    for i in range(n):
        print(a)
        a=a*index
        index+=1
    return



def dispFecNumList(fList):
    index=1
    for x in range(fList):
        print(f"n={index} :n!=")
        index+=1  
    return


dispFecNumList(getFacNumList(8))
but the output show
1
2
6
24
120
720
5040
40320
Traceback (most recent call last):
  File "c:\User\Desktop\python", line 21, in <module>
    dispFecNumList(getFacNumList(8))
  File "c:\Users\Desktop\python", line 15, in dispFecNumList
    for x in range(fList):
TypeError: 'NoneType' object cannot be interpreted as an integer
How can i make the function like
n=1 :n!=1
n=2 :n!=2
n=3 :n!=6
n=4 :n!=24
n=5 :n!=120
n=6 :n!=720
n=7 :n!=5040
n=8 :n!=40320
These two functions can work successfully in independence but i don't know how to combine them
Reply
#2
This is under “General Coding Help” so it obviously is not homework. Therefore I suggest for starters have a look to factorial in math built-in module. Generally speaking there is no need to invent wheel.

EDIT: now it is a homework.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
Following is desired output achieved with built-in tools:

import math

n = 8

for i in range(1, n+1):
    print(f'{i=}, :n!={math.factorial(i)}')

i=1, :n!=1
i=2, :n!=2
i=3, :n!=6
i=4, :n!=24
i=5, :n!=120
i=6, :n!=720
i=7, :n!=5040
i=8, :n!=40320
This should give hint how to refactor code so that desired output can be achieved with two selfwritten functions (note, that math.factorial returns value, not printing it).

I also recommend stick to conventions set in PEP8 about Function and Variable Names
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#4
thank you
Reply


Forum Jump:

User Panel Messages

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