logo CodeStepByStep logo

count_coins

Language/Type: Python file input

Write a function named count_coins that accepts a file name as a parameter. The files' contents is a series of pairs of tokens, where each pair begins with an integer and is followed by the type of coin, which will be "pennies" (1 cent each), "nickels" (5 cents each), "dimes" (10 cents each), or "quarters" (25 cents each), case-insensitively. Add up the cash values of all the coins and print the total money. For example, if the input file coins.txt contains the following text:

3 pennies 2 quarters 1 Pennies 23 NiCkeLs 4 DIMES

The call of count_coins("coins.txt") should produce the following output:

Total money: $2.09

You may assume that the file contains at least 1 pair of tokens. You may also assume that the input is valid; that the input has an even number of tokens, that every other token is an integer, and that the others are valid coin types.

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.