Python Forum
Skipping a line on output - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Skipping a line on output (/thread-3057.html)



Skipping a line on output - lucas0150 - Apr-27-2017

Hi.

Sorry for the poor English, I'm not a native speaker.

The boring code I'm writing works as follows. The user inputs a number T. The user is then prompted, T times, to provide a couple of values A and N, such that the N integers that come after A and their sum will be printed as the output. I've worked everything out just fine, but I'm supposed to get the sequence of integers in a line and their sum on another line, otherwise the automatic corrector my retarded professor uses won't accept it. To print the sequence in the same line, I used the argument end = ' '. However, this argument makes everything that comes after it appear on a single line, and the sum is supposed to appear in a new line. What I mean is, if you type 1 3, you're supposed to get

1 2 3
6

But my program gives

1 2 3 6

So, how do I make the sum of the numbers appear on a new line? Here's my code. The three final lines are what's really important.

T = int(input('Number of pairs: '))

lizt = []

for i1 in range(1, T):
    A, N = input('Insert a couple of numbers').split()
    A, N = int(A),int(N)
    for i2 in range(A, A+N):
        lizt.append(i2)
        print(i2, end =' ')
    sm = sum(lizt) 
    print(sm)



RE: Skipping a line on output - Larz60+ - Apr-27-2017

The end = ' ' causes no line feed to be issed
use a formatting statement as follows:
print('\nSum of numbers: {}'.format(sm))



RE: Skipping a line on output - lucas0150 - Apr-27-2017

(Apr-27-2017, 01:56 AM)Larz60+ Wrote: The end = ' ' causes no line feed to be issed
use a formatting statement as follows:
print('\nSum of numbers: {}'.format(sm))

Oh, that works. Thanks a lot man Big Grin


RE: Skipping a line on output - Ofnuts - Apr-27-2017

Using \n at the beginning of print() statements can create problems because console output actually produces an output when it sees the \n (line buffering). Using a print() without arguments will add a linefeed in the output.

IMHO the OP would have better used a join() instead of multiple print(...) anyway.