Link to home
Start Free TrialLog in
Avatar of nav2567
nav2567Flag for United States of America

asked on

python script assistance

Hello,

I have written the below to find factorial of a number.  But the printing part is not great.  For example, instead of 3! is 6, I am getting 3 ! is 6 (with a space before !).  

May someone advise how I could fix this?  

Thanks

def factorial(n):
    total = 1
    for i in range(1, n+1):
        print(i)
        total = total * (i)
    return total

num = int(input("Please enter a number "))
print(num,"! is ",factorial(num))
ASKER CERTIFIED SOLUTION
Avatar of Antzs
Antzs
Flag of Malaysia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Norie
Norie

Try this.
def factorial(n):
    total = 1
    for i in range(1, n+1):
        print(i)
        total = total * (i)
    return total

num = int(input('Please enter a number '))
print(f'{num}! is {factorial(num)}')

Open in new window

Avatar of nav2567

ASKER

Thanks All.  

Norie, would you explain what does print(f'{...}...{....}') do? 
f'.....'  is a format string.
{xxx}   mean variable xxx should be inserted.
{yyy(xxx)}  means the return value of  function yyy with argument xxx should be printed.