<activity xml:id="forloop-1" >
<title>Exercise (algorithm and implementation)</title>
<statement>
<p> Come up with an algorithm for a program that calculates
the average grade for three students.
The program should prompt the instructor to enter a
grade, then another, the one more, in a loop.
The program should then output the
average grade for the section.
</p>
<p>
Sample Input/Output
</p>
<program language="python">
How many grades: 3
Enter grade: 95.0
Enter grade: 85.0
Enter grade: 90.0
Class Average: 90.0
</program>
</statement>
<program label="10-6-1x" interactive="activecode" language="python">
<code>
def main():
# This part handles the interaction
count = int(input("How many grades: "))
grades = []
for i in range(count):
grades.append(int(input("Enter grade: ")))
avg = sum(grades)/len(grades)
print(f"Average is: {avg}")
main()
====
from unittest.gui import TestCaseGui
from unittest.mock import patch
from io import StringIO
import unittest
class TestGrader(TestCaseGui):
@patch('builtins.input')
@patch('sys.stdout', new_callable=StringIO)
def test1(self, mock_stdout, mock_input):
"""
Tests main() using mocking.
Inputs: 2 grades, 10 and 20.
Expected Output: Average is: 15.0
"""
# Define the sequence of inputs
# 1st: count (2), 2nd: grade (10), 3rd: grade (20)
mock_input.side_effect = ['2', '10', '20']
# Call the function
main()
# Get the printed output and strip whitespace/newlines
output = mock_stdout.getvalue().strip()
# Verification
self.assertEqual(output, "Average is: 15.0")
</code>
</program>
<solution>
<program language="python">
<code>
def main():
# This part handles the interaction
count = int(input("Enter number of grades: "))
grades = []
for i in range(count):
grades.append(int(input("Enter grade: ")))
avg = sum(grades)/len(grades)
print(f"Average is: {avg}")
main()
</code>
</program>
</solution>
</activity>
When we compile and run this, we get the following output:
"An error occurred at the end of your code"
See the attached image.
Can you please point us in the right direction? We ran this using regular Python unittests locally on our machine, and we believe the unittests should work. Thank you!
-Suzanne
--
You received this message because you are subscribed to the Google Groups "PreTeXt support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pretext-suppo...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/pretext-support/91177c71-a464-4046-a33a-9c76703f0551n%40googlegroups.com.
<image (1).png>