Python data text file grades program

2024/11/17 3:40:23

Looking for help with my program. There is a text file with 5 first and last names and a number grade corresponding to each person. The task is to create a user name and change the number grade to a letter grade. The problem I am having is that the program is only outputting one line (or username) from the textfile and not all 5 lines (usernames).

def main():inFile = open("grades.txt", "r")aList = inFile.readlines()grades = ["F","E","D","C","B","A"]

revised so that program will run

    for lines in aList:n = lines.split()print(n[0][0].lower()+ n[1][0:4].lower()+ "001 "+ grades[eval(n[2])])inFile.close()main()

If anyone could point out where I am making the mistake it would be greatly appreciated.

Answer

OK... trying not to give you the obvious answer, here. Think about what you want to do : you want to do something "for each lines of aList". That is the meaning of :

for lines in aList:

In other words, everything in the "for" block, after this very line, will execute for each element inside aList... provided it is in the block, so properly indented.

For example :

for a in [1,2,3]:print(a)

Will display 1,2,3. "print(a)" is in the for block, as it is properly indented.

However :

for a in [1,2,3]:x = aprint(x)

Here, "print(x)" is NOT in the block. So what is going to happen here ? x will receive the value of 1, then 2, then 3. And only after that will x be printed, with the last value it received.

With that in mind, you should be able to spot what's wrong with your code.

(BTW, in order to read your file more safely and more "pythonically", you should have a look at this : https://docs.python.org/2/tutorial/inputoutput.html#methods-of-file-objects, particularly the end of the paragraph and the "with" idiom).

https://en.xdnf.cn/q/120248.html

Related Q&A

how to fill NA with mean only for 2 or less consequective values of NA

I am new to python. please help me how I should proceed. The following dataframe contains large blocks of NaNs. # Fill the NAs with mean only for 2 or less consecutive values of NAs. # Refer to the d…

Build a new dictionary from the keys of one dictionary and the values of another dictionary

I have two dictionaries:dict_1 = ({a:1, b:2,c:3}) dict_2 = ({x:4,y:5,z:6})I want to take the keys from dict_1 and values from dict_2 and make a new dict_3dict_3 = ({a:4,b:5,c:6})

Python 2.7 - clean syntax for lvalue modification

It is very common to have struct-like types that are not expected to be modified by distant copyholders.A string is a basic example, but thats an easy case because its excusably immutable -- Python is …

Python - Global name date is not defined [closed]

Closed. This question needs debugging details. It is not currently accepting answers.Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to repro…

Python update user input with tkinter button

Im just starting with python and im having a problem. Ive tried various solutions, but i cant update the field that says 19. When i click on plus, i want it to be 20, then 21,... and when i click - it …

Python Unique DF in loop [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 5…

TypeError: unsupported operand type(s) for +=: NoneType and str

I am new to Python and Im sure that Im doing something wrong - I would like to ask a user for three numbers and print their sum, heres my current code:for i in range(0, 3):total = Nonenum = input(Pleas…

multiply list of ndarrays by list

I want to multiply a list of numbers, by a ndarray, that is, each number in the list multiply by the first ndarray. This is the list: list1 = [840,845,897]This is the list of ndarray list = df[Example]…

Python skipping last element in while iterating a list using for loop [duplicate]

This question already has answers here:Strange result when removing item from a list while iterating over it in Python(12 answers)Closed 9 years ago.class Sample:def __init__(self):self.lst_report_foot…

can this code be shortened or improved? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.This question does not appear to be about programming within the scope defined in the help center.Cl…