I need help with just one line. I need an "F" to be displayed on the same line the student with 45 points. I keep getting the "F" to display on a new line below the 45. I guess something is wrong with my if statement. I also need to display the % symbol after the 83.3. I'm using python 3.5.1. Thank you so much for any help.

def main():
    file_students = open("student_points.txt" , "r")
    stu_name = file_students.readline()

    num_stu = 0
    f_students = 1
    pass_students = 5/6

    print("Student\t\t\tPoints\t\tGrade")
    print("-------------------------------------\n")
    while stu_name != "":
        stu_name = stu_name.rstrip("\n")
        stu_points = file_students.readline()
        stu_points = int(stu_points)
        print(stu_name, "\t\t", stu_points, sep="")
        num_stu += 1

        if stu_points < 60:
            print("\t\t\t\t\tF")

        stu_name = file_students.readline()

    file_students.close()
    print()
    print("Number of students processed=", num_stu)
    print("% of students who passed=" , format(pass_students * 100 , ".1f"))

main()

The students and their corresponding grades are:
Johnson Smith: 93
Maryanne James: 80
Stanton Chase: 45
Mildred Morris: 90
George Deitz: 89
Maisie Kling: 79

Recommended Answers

All 3 Replies

My python isn't very good but if I remember correctly you can put a comma ( , ) at the end of the first print statement and it will not output a new line, making the second print appear beside the first.
if not, just concatenate your full line (inlucding the 'F' if it is required) into a string and print once.

@hericles The comma trick works only in python 2. With python 3, print is a full fledged function and the way to prevent the newline is to use the parameter end='' in the call to print() .

One way to do this is to have your if statement be linked to a string variable and set the string to "F" if the value is less than 60 and setting it to "" if it isn't.

if stu_points < 60:
(tab) Fstring = "F"
else:
(tab) Fstring = ""

(for some reason my client didn't let me have the option of using the "code" feature, so this is the best I could do in regards to white space :( sorry )

Not too sure what 'sep' is in your code, but I guess you could just use sep as your variable, also you would need to have this if statement before your print statement, or else you will be calling the variable before it has been set.

Sidenote: this is my favorite printf trouble ever!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.