Home > Software engineering >  How can i remove this extra "*" from the end
How can i remove this extra "*" from the end

Time:01-22

help me how can i remove that extra "*" at the end

num = int(input("Enter number :"))
factorial = 1
if num==0:
    print(" 1")

then i used else and started a loop

else:
    for i in range (1,num 1):
        factorial = factorial * i
        
        print(i,end="*")
    
    print ("\n=",factorial)

when run the code you can see the output in which at last you can see that extra"" symbol how can i remove only that extra "" symbol. As i don't have much reputation I can't use image to show you.

CodePudding user response:

An easy way can be to print 1 outside the loop, then change range condition to start from 2. Then start by printing the asterisk * first, rather than at the end:

num = int(input("Enter number :"))
factorial = 1
if num==0:
    print(" 1")
else:
    print(1, end='')

    for i in range(2, num   1):
        factorial = factorial * i

        print(end=f'*{i}')

    print("\n=", factorial)

CodePudding user response:

you can print "*" along side numbers but that should be less than the number of iterations

num = int(input("Enter number :"))
factorial = 1
if num==0:
    print(" 1")
else:
    for i in range (1,num 1):
        factorial = factorial * i
        print(i,end="")
        if(i<num):
            print("*",end="")
    
    print ("\n=",factorial)
  •  Tags:  
  • Related