John L.

asked • 07/27/24

Requesting help on one assignment

I'm having some trouble with the problem, the instruction states to create another file that had the following text, which I did using Python IDLE which then I saved. But I do not understand what I did wrong since it states the file does not exist/work. I am requesting a video on how to solve this problem and also accepts a file name as a parameter and where to save as well.


Write a function called 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 a girl; you may not assume that it contains an even number of names. For example, if the input file contains the following text::

Dan 3

Cordelia 7

Tanner 14

Mellany 13

Curtis 4

Amy 12

Nick 6

Then the function 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


2 Answers By Expert Tutors

By:

Patrick M. answered • 07/28/24

Tutor
New to Wyzant

Experienced Biochemistry Tutor

Michael D.

tutor
Curious if you're coming from a C or similar background [I've been there] given the way you've written the loop. FYI, you can use `enumerate(F, start=1)` instead of manually adding to enumeration counter. Much more readable and "Pythonic" as the cool kids would say. Also, it's well worth your time to learn about f-strings.
Report

07/28/24

Patrick M.

f-strings are often a little confusing to a beginner so I tried to choose a more bare bones way to think about strings. I did not know about being able to start enumerate like that, thanks for the tip. Python is actually my first and only programming language believe it or not.
Report

07/28/24

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.