Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
This is the main function i have:
Python
<pre lang="Python">def response(res):
    global score, correct
    res = res.upper()
    if res == correct:
        score = score + 1
        return "Correct Answer!"
    else:
        print("Wrong Answer :(")
        return f"Correct Answer: {correct}"


What I have tried:

This is the test function i created:
Python
def test_response():
    global correct
    global score
    correct = "A"
    score=0
    # Test case 1: When the provided answer is correct
    assert response("A") == "Correct Answer!"
    assert score == 1

    # Reset score
    score = 0

    # Test case 2: When the provided answer is incorrect
    assert response("wrong") == f"Correct Answer: {correct}"
    assert score == 0


and upon calling this is the assertion error i am facing:
in test_response
    assert response("A") == "Correct Answer!"
AssertionError
Posted

Your error suggests that, when you check the test case for the correct answer '"A"', it is not returning the result as expected.

Your issue is that in the 'test_response' function, when the correct answer is provided '"A"', your assertion is failing. The code below should be correct, assuming that the correct variable is indeed set to '"A"' before running your test case.

Also double-check that the correct variable is set correctly and that there are no typos. You may also want to print the value of 'response("A")' to see what it is returning and debug from there -
Python
def response(res):
    global score, correct
    res = res.upper()
    if res == correct:
        score = score + 1
        return "Correct Answer!"
    else:
        print("Wrong Answer :(")
        return "Correct Answer: {}".format(correct)

def test_response():
    global correct
    global score
    correct = "A"
    score = 0

    # Test case 1: When the provided answer is correct
    assert response("A") == "Correct Answer!"
    assert score == 1

    # Reset score
    score = 0

    # Test case 2: When the provided answer is incorrect
    assert response("wrong") == "Correct Answer: {}".format(correct)
    assert score == 0
 
Share this answer
 
v2
Comments
CPallini 1-Dec-23 13:59pm    
My 5.
Maciej Los 1-Dec-23 17:46pm    
5ed!
Andre Oosthuizen 2-Dec-23 3:03am    
Thank you, much appreciated!!
The following program
Python
def response(res):
  global score, correct
  res = res.upper()
  if res == correct:
    score = score + 1
    return "Correct Answer!"
  else:
    print("Wrong Answer :(")
    return f"Correct Answer: {correct}"



def test_response():
  global correct
  global score
  correct = "A"
  score=0

  # Test case 1: When the provided answer is correct
  assert response("A") == "Correct Answer!"
  assert score == 1

  # Reset score
  score = 0

  # Test case 2: When the provided answer is incorrect
  assert response("wrong") != "Correct Answer!"
  assert score == 0


test_response()
produces the (expected) output
Wrong Answer :(

Anyway, I just changed the assertion for the test case 2 (which is irrelevant to your problem).
 
Share this answer
 
Comments
Andre Oosthuizen 1-Dec-23 11:24am    
Not sure why the 1 vote... my +5.
CPallini 1-Dec-23 13:58pm    
Thank you.
Maciej Los 1-Dec-23 17:46pm    
5ed!
CPallini 2-Dec-23 1:44am    
Thank you!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900