Kevin
Kevin

Reputation: 31

How can I modify the syntax end=' ' to print on the next line of while loops?

I'm writing a simple code to print the factor of a number from the user input. However, the next line of user input starts on the same line as the indexes of previous loop.

while True: 
    num = int(input('enter a number: '))
    print('factor of {}: '.format(num), end = ' ')
    for i in range (1,num+1) : 
        if num%i == 0 : 
            print(i, end = ' ')

Output:

enter a number: 32
factor of 32:  1 2 4 8 16 32 enter a number: 32
factor of 32:  1 2 4 8 16 32 enter a number: 32
factor of 32:  1 2 4 8 16 32 

Upvotes: 0

Views: 41

Answers (2)

Syed Shahzer
Syed Shahzer

Reputation: 346

You need to prepend \n to the input line.

    while True: 
        num = int(input('\nenter a number:'))
        print('factor of {}: '.format(num), end = ' ')
        for i in range (1,num+1) : 
            if num%i == 0 : 
                print(i, end = ' ')

Output

enter a number:32
factor of 32:  1 2 4 8 16 32 
enter a number:32
factor of 32:  1 2 4 8 16 32 
enter a number:

Upvotes: 1

Nabil
Nabil

Reputation: 1278

You can another print after the for loop

Upvotes: 1

Related Questions