nav2567
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))
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks All.
Norie, would you explain what does print(f'{...}...{....}') do?
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.
{xxx} mean variable xxx should be inserted.
{yyy(xxx)} means the return value of function yyy with argument xxx should be printed.
Open in new window