-1

Update: It's coming along! There are still a couple problems with it though. I need the line numbers and commendation asterisk to print on the same line as the score, which is not happening.

Here is the code:

def main():
    fil_inp_stu = open("student_test_scores.txt", "r")

    #Variables
    num_rec = 0
    num_com = 0
    per_com = 0
    total_scores = 0
    avg_scores = 0
    
    print("#\tScore\tCommendation\n----------------------------")

    one_score = fil_inp_stu.readline()
    
    while one_score != "":

        one_score_int = int(one_score)
        print("\t", one_score_int)

        num_rec = num_rec + 1
        print(f"{num_rec}:")
       
        one_score = fil_inp_stu.readline()

        total_scores += one_score_int
        avg_scores = total_scores / num_rec

        per_com = num_com / num_rec

    
        num_com = one_score_int >= 100
        while num_com:

          print("\t\t\t*")
          break 
    
   
      

    print(f"\nNumber of records: {num_rec}")
    print(f"Average test score: {avg_scores:.2f}")
    print(f"Number of commendations: {num_com}")
    print(f"Percentage of commendations: {per_com:.2%}")
     
    fil_inp_stu.close()

main()

Here is the output:

#   Score   Commendation
----------------------------
     69
1:
     9
2:
     129
3:
            *
     131
4:
            *
     146
5:
            *
     109
6:
            *
     71
7:
     69
8:
     18
9:
     129
10:
            *
     94
11:
     53
12:
     25
13:

Number of records: 13
Average test score: 80.92
Number of commendations: False
Percentage of commendations: 0.00%

Thanks for the help so far guys

Pranav Hosangadi
  • 23,755
  • 7
  • 44
  • 70
  • #1 - you can make a variable that's a number and you can add to it. e.g. `val = 0` `val += 1` `print(val)`. Can you see how that'd solve #1? – Macattack Dec 02 '20 at 00:06
  • Does this answer your question? [How to print without newline or space?](https://stackoverflow.com/questions/493386/how-to-print-without-newline-or-space) – Pranav Hosangadi Dec 02 '20 at 18:53

1 Answers1

0

Is this what you're looking for? Not sure I understood correctly.

def main():
    print("#\tScore\tCommendation\n----------------------------")

    num_records = 0
    total_scores = 0

    with open("student_test_scores.txt", "r") as fil_inp_stu:
        one_score = fil_inp_stu.readline()

        while one_score != "":
            num_records += 1

            one_score_int = int(one_score)
            total_scores += one_score_int

            if one_score_int > 100:
                print(f"\t{one_score}*")
            else:
                print(f"\t{one_score}")

            one_score = fil_inp_stu.readline()

    avg_score = total_scores // num_records

    print(
        f"Number of records: {num_records}"
        f"Total scores: {total_scores}"
        f"Average score: {avg_score}"
    )


main()

Note that I added a context manager (with statement) to both simplify and make it safer. With both an open and close statement you run the risk of causing problems with your file if your code crashes before reaching the .close() line.

Rolv Apneseth
  • 2,078
  • 2
  • 7
  • 19