logo CodeStepByStep logo

flip_coins

Language/Type: Python file input

Write a function named flip_coins that accepts as its parameter a string for an input file name. Assume that the input file data represents results of sets of coin flips that are either heads (H) or tails (T) in either upper or lower case, separated by at least one space. Your function should consider each line to be a separate set of coin flips and should output to the console the number of heads and the percentage of heads in that line, rounded down to the nearest integer (truncated). If this percentage is more than 50%, you should pra "There were more heads!" message. For example, consider the following input file:

H T H H T
T t   t T h  H
   h

For the input above, your function should produce the following output:

3 heads (60%)
There were more heads!

2 heads (33%)

1 heads (100%)
There were more heads!

The format of your output must exactly match that shown above. You may assume that the file contains at least 1 line of input, that each line contains at least one token, and that no tokens other than h, H, t, or T will be in the lines.

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.