Python Forum
Skipping a line on output
Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Skipping a line on output
#1
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)
Reply
#2
The end = ' ' causes no line feed to be issed
use a formatting statement as follows:
print('\nSum of numbers: {}'.format(sm))
Reply
#3
(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
Reply
#4
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.
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Seven Segment Display - QUERY on Multi-line side-by-side printing output ajayachander 3 4,758 Mar-13-2020, 07:02 AM
Last Post: ajayachander
  reading text file and writing to an output file precedded by line numbers kannan 7 10,246 Dec-11-2018, 02:19 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020