1

Hi, I'm working on this problem in my python programming class and need help with part of it. I need to have it display a "F" for anyones grade that is below 60, and also have it display the amount of "F" students, as well as the percentage of students who passed. My code correctly displays the points earned by each student but i can't figure out the rest.
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
My code is as follows:

def main():
    file_students = open("student_points.txt" , "r")
    stu_name = file_students.readline() #read the first record
                                        #which is the first student's name
    num_stu = 0

    print("Student\t\t\tPoints\t\tGrade")
    print("--------------------------------\n")
    while stu_name != "":
        stu_name = stu_name.rstrip("\n")    #strip \n from name
        stu_points = file_students.readline() #read points
        stu_points = int(stu_points)     #cast points into an int

        print(stu_name, "\t\t", stu_points, sep="")
        num_stu += 1

        stu_name = file_students.readline()


    file_students.close()
    print()
    print("Number of students processed =", num_stu)

main()

Thanks for any help

3
Contributors
11
Replies
76
Views
10 Months
Discussion Span
Last Post by Freshly
Featured Replies
  • Here https://docs.python.org/3/reference/expressions.html?highlight=precedence#value-comparisons are the comparison operators in python. You must write if stu_points < 60: print("F") Read More

  • Is this what you were trying to do? def main(): f = open('student_points.txt', 'r') contents = [] for line in f: contents.append(line) f.close() print('Student\t\t\tPoints\t\tGrade') print('----------------------------------------------\n') num_stu = 0; num_passed = 0 for content in contents[:]: num_stu += 1 stu_name = content.split(':')[0] stu_points = content.split(':')[1] print(stu_name + '\t\t' + stu_points.strip('\n'), end='') … Read More

  • > Which version of python are you using? I'm using 3.5.1 and this code does not work I'm using Python version 3.5.1. What do you mean the code does not work? Please describe the symptoms so I can at least have a *chance* of solving your problem. "This code does … Read More

  • The print function of Python 3 adds a newline character ('\n') by default. To get the F to display on the same line, pass line 14 `print(stu_name, "\t\t", stu_points, sep="")` from your code above the keyword argument `end=""` like this `print(stu_name, "\t\t", stu_points, "\t", sep="", end="")`. >>> while True: # … Read More

0

@Gribouillis I keep getting invalid syntax for putting

if stu_points is 60>:
    print("F")

I don't understand what is invalid

0

@Gribouillis I keep getting the "F" to go on a separate line, do you know how I could get the F to display on the same line as "45"? Thank you

1

Is this what you were trying to do?

def main():
    f = open('student_points.txt', 'r')
    contents = []

    for line in f:
            contents.append(line)

    f.close()

    print('Student\t\t\tPoints\t\tGrade')
    print('----------------------------------------------\n')

    num_stu = 0; num_passed = 0
    for content in contents[:]:
            num_stu += 1
            stu_name = content.split(':')[0]
            stu_points = content.split(':')[1]
            print(stu_name + '\t\t' + stu_points.strip('\n'), end='')
            if int(stu_points) < 60:
                    print('\t\tF')
            else:
                    print('')
                    num_passed += 1

    print('')
    print('Number of students processed =', num_stu)
    print('Number of students who passed = %.0f' % (num_passed / num_stu * 100) + '%')

if __name__ == '__main__':
    main()

This is the output:

Student         Points      Grade
----------------------------------------------

Johnson Smith        93
Maryanne James       80
Stanton Chase        45     F
Mildred Morris       90
George Deitz         89
Maisie Kling         79

Number of students processed = 6
Number of students who passed = 83%
1

Which version of python are you using? I'm using 3.5.1 and this code does not work

I'm using Python version 3.5.1. What do you mean the code does not work? Please describe the symptoms so I can at least have a chance of solving your problem. "This code does not work" is the most unhelpful problem report that you could ever devise.
And here's to show that it does work. https://youtu.be/dkgTJIwJMyA

0

@Freshly is something wrong with my if statement in my code? The "F" keeps displaying itself on a new line below the 45. I appreciate you trying to help me.

1

The print function of Python 3 adds a newline character ('\n') by default. To get the F to display on the same line, pass line 14 print(stu_name, "\t\t", stu_points, sep="") from your code above the keyword argument end="" like this print(stu_name, "\t\t", stu_points, "\t", sep="", end="").

>>> while True: # The print will add a newline character and print the F on the next line
...     print(stu_name, "\t\t", stu_points, sep="")
...     if stu_points < 60:
...         print("F")
...     break
... 
Stanton Chase       45
F
>>> while True: # Passing the keyword argument end="" to print will print F on the same line
...     print(stu_name, "\t\t", stu_points, "\t", sep="", end="")
...     if stu_points < 60:
...         print("F")
...     break
... 
Stanton Chase       45  F
>>>

Edited by Freshly

Comments
Nevermind I finally got it, thank you.
0

@Freshly I'm about to give up, I literally typed this into my code and it's still not working. Now it only displays the first name in the list. I dont understand what's wrong. Nothing I put seems to work, so frustrating.

0

@Freshly I put the line you suggested and now it does put the 45 on the same line but it also repeats the other names. Please send file to me?

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.