Question: 6 . 1 5 LAB: Format List Items Into Prices Learning Goals Practice looping over a collection to work with each element from the collection

6.15 LAB: Format List Items Into Prices
Learning Goals
Practice looping over a collection to work with each element from the collection
Review CHALLENGE ACTIVITY 6.5.2: For loop: Printing a list. to see how to
turn a string into a list
use f-strings to format a number into a price
Create a function with the requested parameter and returns a formatted list.
Instructions
Function
Create a function format_as_price(), with prices_str as a parameter. The function returns a list that stores each price formatted as a string with a dollar sign at the start and 2 decimal places.
Inside the function, turn the text stored in the prices_str into a list.
Loop over the elements in the list and convert each string into a float. (Assume that each item in the string is a valid float.)
Finally, turn each float into a string, formatted as a price (i.e., with 2 decimal places) that also has a $ at the start.
Add each formatted string to the list that's returned at the end of the function.
Main program
In your main program:
Take a string as input: this is the text that is input into the function.
Call the function format_as_price() to get the formatted list into your main program.
Output each item on the list
Testing your code
Input
34.6276.3085.05
The function returned
['$34.62','$76.30','$85.05']
Main Program Output
$34.62
$76.30
$85.05
Hints / Troubleshooting
Remember that an f-string can help with outputting 2 decimal places using f"{...:.2f}".
If the "Unit Test" fails saying that "We ran into an error while running your code. Python says: could not convert string to float: '.'" refer to the instructions:
what is the type of the input into the function?
what does the main program need to collect and do?
what is the function supposed to return?

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!