Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why is my replacement only producing the last value in a list that I have passed through a function?

I have a list (letter_grades) that has four "X" in it (each corresponding to a student grade). I need to use a function to calculate letter grades from a previously existing list (student_grades = [98, 87, 67, 89]). Finally, I want to take that information and replace the "X" values in letter_grades with the values I got using student_grades.

So, if I calculated that the first student scored 98, the function told me that was an A, I want to replace the X with the A (in the correct order). In theory I’d end up with letter_grades = A, B, D, B.

I feel like I am really close (but maybe not). Here is what I have so far. My issue is that it is only changing the values to the last item in the list. I assume it’s because of how print works, but I am having trouble figuring out how else to solve this.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

student_grades = [98, 87, 67, 89]
letter_grades = ['X','X','X','X']

def Gradecalc(list):
    if score >= 90:
        letter = 'A'
    else:   
        if score >= 80:
            letter = 'B'
        else:  
            if score >= 70:
                letter = 'C'
            else:    
                if score >= 60:
                    letter = 'D'
                else:
                    letter = 'F'
    return letter

for value in range(len(letter_grades)):
        letter_grades[value]=letter_grades[value].replace("X", Gradecalc(student_grades))
    
print(letter_grades)

The output just gives me [‘B’, ‘B’, ‘B’, ‘B’] – which is the last anticipated value of letter_grades.

Am I close? How do I get it to print the correct values?

>Solution :

Actually, your code snippet produces errors.
As mentioned in the comment, score used but not defined.

Here’s how your code can be changed:

student_grades = [98, 87, 67, 89]
letter_grades = ['X','X','X','X']

def Gradecalc(score):
    if score >= 90:
        letter = 'A'
    elif score >= 80:
        letter = 'B'
    elif score >= 70:
        letter = 'C'
    elif score >= 60:
        letter = 'D'
    else:
        letter = 'F'
    return letter

for value in range(len(letter_grades)):
    letter_grades[value] = letter_grades[value].replace("X", Gradecalc(student_grades[value]))
    
print(letter_grades)

Output:

['A', 'B', 'D', 'B']

I changed the argument of Gradecalc to score.

Then changed the calling of Gradecalc(student_grades) to Gradecalc(student_grades[value]) (only one grade, not all of them at one time).

Also, nested if..else can be rewritten as if..elif..else, makes code more readable.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading