Creating a function to process through a .txt file of student grades

2024/11/15 16:24:38

Can someone help...

My driver file is here:

from functions import process_marks def main():try:f = open(argv[1])except FileNotFoundError:print("\nFile ", argv[1], "is not available")exit()process_marks(f)f.close()main()

I'm trying to modify process_marks(f) to run my code. and produce this (from file that runs without functions):

Names of students who have written tests:
Anthony Austyn Bronson Conor Mark
Enter name of student whose test results you wish to see: AnthonySummary of Test Results for Anthony
===================================
Test scores:  85 85 85 85
Number of tests written ..................  4

This is what I currently have:

names = []
name_print = "\nSummary of Test Results for "def process_marks(file_name):namesfor line in file_name:names.append(line.split()[0])print('\nNames of students who have written tests:')print(*sorted(names), sep=' ')name = input('Enter name of student whose test results ''you wish to see: ')check_name(name)parts = line.split()if parts[0] == name:print_scores(name)def check_name(person):if person not in names:print('\nNo test data found for ', person)input("Press Enter to continue ...")else:print(name_print + person)        def print_scores(person, parts):print('=' * ((len(name_print)) - 1))test_scores = ' '.join(parts[1:])print('Test scores: ', end=' ')print(test_scores)

Which outputs:

Names of students who have written tests:
Anthony Austyn Bronson Conor Mark
Enter name of student whose test results you wish to see: AnthonySummary of Test Results for Anthony

I need help making print_scores() function work in process_marks().

Can someone see where my errors lie?

Answer

Your error (mainly) lies in the fact that you are comparing the input name to the last row of the file. This is because you check if parts[0] == name where parts = line.split(). This means the parts are of the last row of the file always - no matter what name provided.


To fix this, I would start by a better way of storing your data. Right now you are just saving the names in a list. But what about the grades? I think a better solution would be to use a dict. So start by changing to:

names = {}  # instead of []

Now you want to fill that dict with the names as keys (keeps the logic similar as with the list) and the list of grades as the value for that key. So your file parsing can look like:

for line in file_name:elements = line.split()names[elements[0]] = elements[1:]
# names now look like: {'Anthony': ['85', '85', '85', '85'], 'Conor': [...], ... }

Now another thing is that you call the check_name function but then make another check in process_marks. This seems redundant. I would change the ceck_name function to return a boolean indicating if the name is ok or not:

def check_name(person):if person not in names:print('\nNo test data found for ', person)input("Press Enter to continue ...")return Falseelse:print(name_print + person)return True

And in process_marks you can use it as:

name = input('Enter name of student whose test results you wish to see: ')
if check_name(name):print_scores(name)

Lastly, regarding the parts issue. Now you have the grades stored in names along with the matching name they belong to. So all we have left to do is change print_scores to take only one argument grades and use that instead of parts, and from process_marks just call it:

print_scores(names[name])

A view of a possible complete code:

names = {}
name_print = "\nSummary of Test Results for "def process_marks(file_name):with open(file_name) as f:for line in f:elements = line.split()names[elements[0]] = elements[1:]print(names)print('\nNames of students who have written tests:')print(*sorted(names), sep=' ')name = input('Enter name of student whose test results you wish to see: ')if check_name(name):print_scores(names[name])def check_name(person):if person not in names:print('\nNo test data found for ', person)input("Press Enter to continue ...")return Falseelse:print(name_print + person)return Truedef print_scores(grades):print('=' * (len(name_print) - 1))test_scores = ' '.join(grades)print('Test scores: ', end=' ')print(test_scores)
https://en.xdnf.cn/q/119637.html

Related Q&A

Python Reddit PRAW get top week. How to change limit?

I have been familiarising myself with PRAW for reddit. I am trying to get the top x posts for the week, however I am having trouble changing the limit for the "top" method. The documentatio…

I want to convert string 1F to hex 1F in Python, what should I do?

num="1F" nm="1" nm1="2" hex(num)^hex(nm)^hex(nm1)I wrote it like the code above, but hex doesnt work properly. I want to convert the string to hexadecimal, and I want an x…

How to call a function in a Django template?

I have a function on my views.py file that connects to a mail server and then appends to my Django model the email addresses of the recipients. The script works good. In Django, Im displaying the model…

Remove duplicates from json file matching against multiple keys

Original Post = Remove duplicates from json dataThis is only my second post. I didnt have enough points to comment my question on the original post...So here I am.Andy Hayden makes a great point - &quo…

Merging CSVs with similar name python [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 8…

urllib.error.HTTPError: HTTP Error 403: Forbidden

I get the error "urllib.error.HTTPError: HTTP Error 403: Forbidden" when scraping certain pages, and understand that adding something like hdr = {"User-Agent: Mozilla/5.0"} to the h…

Compare multiple file name with the prefix of name in same directory

I have multiple .png and .json file in same directory . And I want to check where the files available in the directory are of same name or not like a.png & a.json, b.png & b.json

how to make a unique data from strings

I have a data like this . the strings are separated by comma."India1,India2,myIndia " "Where,Here,Here " "Here,Where,India,uyete" "AFD,TTT"What I am trying…

How to read complex data from TB size binary file, fast and keep the most accuracy?

Use Python 3.9.2 read the beginning of TB size binary file (piece of it) as below: file=open(filename,rb) bytes=file.read(8) print(bytes) b\x14\x00\x80?\xb5\x0c\xf81I tried np.fromfile np.fromfile(np…

How to get spans text without inner attributes text with selenium?

<span class="cname"><em class="multiple">2017</em> Ford </span> <span class="cname">Toyota </span>I want to get only "FORD" …