Amendo
Amendo

Reputation: 89

Printing each iteration horizontally in different lines

guys. I have a for loop and I managed to print the for loop horizontally

def triangular(number):
    for i in range (1, number+1):
        if number%i==0:
            print(number/i, end=" ")

This is what i get

15.0 5.0 3.0 1.0

However, if I try to use the function with different numbers, the code keeps printing it in the same line

triangular(1) 
triangular(3)

1.0 3.0 1.0 [Finished in 0.2s]

How can I do to print the results in a different line, every time I call the function?

Thanks in advance.

Upvotes: 0

Views: 115

Answers (2)

geckos
geckos

Reputation: 6279

Insert a print at the end of the function

def triangular(number):
    for i in range (1, number+1):
        if number%i==0:
            print(number/i, end=" ")
    print("")

Upvotes: 1

sdotson
sdotson

Reputation: 820

You could print a return character \n after your loop like so:

def triangular(number):
    for i in range (1, number+1):
        if number%i==0:
            print(number/i, end=" ")
    print('\n')

Upvotes: 1

Related Questions