logo CodeStepByStep logo


Language/Type: Python file input

Write a function named boy_girl that accepts a file name as a parameter. Your function should read input from that file containing a series of names followed by integers. The names alternate between boys' names and girls' names. Your function should compute the absolute difference between the sum of the boys' integers and the sum of the girls' integers. The input could end with either a boy or girl; you may not assume that it contains an even number of names. For example, if the input file names.txt contains the following text:

Erik 3 
Rita 7 
Tanner 14 
Jillyn 13 
Curtis 4 
Stefanie 12 
Ben 6

Then the call of boy_girl("names.txt") should produce the following console output, since the boys' sum is 27 and the girls' sum is 32:

4 boys, 3 girls
Difference between boys' and girls' sums: 5
Function: Write a Python function as described, not a complete program.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.