0

I have a function that returns a string of student names and maximum grades: Jade: 82. I also have a test function that has a test case that expects the returned value of the function to be a multiline string.

import sys

grades = '''Jade,50,64,82,14
Brenda,90,67,73,80
Chris,75,75,75,75
John,100,85,90,99'''


def maximum(l: list):
    maxVal = 0
    for i in l:
        maxVal = max(maxVal, int(i))
    return maxVal


def max_scores(grades):
    grades = grades.splitlines()
    finalGrades = []
    for i in grades:
        finalGrades.append(i.split(","))
    ans = ""
    for i in finalGrades:
        maxVal = maximum(i[1:])
        ans = ans + i[0] + ": " + str(maxVal)+"\n"
    return ans
max_scores(grades)

Output (without including print)

'Jade: 82\nBrenda: 90\nChris: 75\nJohn: 100\n'

Output (with print in the return statement)

print(max_scores(grades))

Jade: 82
Brenda: 90
Chris: 75
John: 100

The test case

def test_max_scores():
    print("Testing get_max_scores()...", end="")
    grades = '''Jade,50,64,82,14
                    Brenda,90,67,73,80
                    Chris,75,75,75,75
                    John,100,85,90,99'''

    assert((max_scores(grades)) == '''Jade: 82
                                         Brenda: 90
                                         Chris: 75
                                         John: 100''')
    print("... done!")

I am unable to get the function to return the multiline string the test case is expecting.

martineau
  • 119,623
  • 25
  • 170
  • 301
ola
  • 77
  • 1
  • 9
  • What is the last character is the string returned by `max_scores`? What is the last character in `grades`? – rici Dec 31 '21 at 21:00
  • @rici the last character returned by max_scores is "\n". if I execute grades = grades.splitlines() I have a list of the original grade variable and grade[-1] is the last student entry with the last score of 99' – ola Dec 31 '21 at 21:58
  • Right, the last character in `max_scores(grades)` is a newline. Your assert says that is equal to a string literal (which is not actually `grades`, sorry). Is it equal to that string literal? What's the last character of the string literal? – rici Dec 31 '21 at 22:15
  • If I understand your question, the last character of the string literal (the content of the assert) is the last student score (100) 0. So if I'm following you, the last character of 'max_scores(grades)' should be a string literal and not a newline character? – ola Jan 01 '22 at 00:09
  • 1
    Not exactly. You use the assert to check if the result of the call is what you expect it to be. You know that the result of the call ends with a newline. So the expected return value -- the literal string you compare the result with -- should also end with a new line. Of course, the rest of it needs to match as well; there might be a problem with the whitespace or whatever. But my point is that when you write a test function, you need to test against the value which you expect to receive. – rici Jan 01 '22 at 01:22
  • If you don't, your test will fail. Python's string comparison isn't going to decide that the two strings are "close enough" (although you could write a comparison function which did that). Focussing on the last character which just a way of pointing out an obvious reason why the check was bound to fail. In other words, the fact that the function returns a multiline string is irrelevant. The problem is that you are comparing with the wrong thing. – rici Jan 01 '22 at 01:24
  • I figured it out. Just like you said. The problem was where my code returned a newline character after every student score (including the last student) the test case had no newline character after the last student score. `John: 100\n` vs `John: 100`. So I did a replace on the final string just before the return statement to remove the last trailing newline character. Thanks for your help – ola Jan 01 '22 at 08:12

0 Answers0