Python Forum
Printing on same line from 2 functions.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Printing on same line from 2 functions.
#1
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 !
Reply
#2
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 = '')
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
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.
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
  Command line inputs not printing to Log File ijosefson 1 3,316 Oct-19-2017, 06:41 AM
Last Post: buran

Forum Jump:

User Panel Messages

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