Python Forum
Printing on same line from 2 functions. - 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: Printing on same line from 2 functions. (/thread-22921.html)



Printing on same line from 2 functions. - Dyefull - Dec-03-2019

Hey guys quick question,

this is the code that I've written so far:

total = 0

def print_student(student):
    informatie_student = student.split("__________")

    naam_student = informatie_student[0]
    cijfers_student = informatie_student[1]

    print_cijferstudent(cijfers_student)

    print naam_student

def print_cijferstudent(input_cijfer):
    enkel_cijfer = input_cijfer.split(" ")
    cijfer1 = float(enkel_cijfer[0])
    cijfer2 = float(enkel_cijfer[1])
    cijfer3 = float(enkel_cijfer[2])

    gemiddelde = round((total + cijfer3 + cijfer2 + cijfer1) / len(enkel_cijfer), 1)
    print gemiddelde

students = open('input.txt').readlines()

for student in students:
    print_student(student)
the input is: Tom Bombadil__________6.5 5.5 4.5

and the output is:

5.5
Tom Bombadil

I want it so that it will print: Tombadil 5.5. on one line.

Thanks in advance guys !


RE: Printing on same line from 2 functions. - ichabod801 - Dec-03-2019

First of all, you are using Python 2.7. End of life for Python 2.7 is at the end of the month. You should really upgrade to Python 3.7.

To answer your question, to print without going to the next line in 2.7, you use a trailing comma:

for spam in range(5):
    print 'spam',
In 3.0+, print is a function, and you use the end parameter to specify no new line at the end:

for spam in range(5):
    print('spam ', end = '')



RE: Printing on same line from 2 functions. - ndc85430 - Dec-05-2019

You could also just avoid both functions printing. Return a string from one and then construct a single string from that value and the other one and print it.