简体   繁体   中英

How can i remove this extra "*" from the end

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.

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)

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM