Ex7 - If Statements and Conditional Tests:
Objective: Read data from a text file and assign prices for certain items using a conditional test.
Description: Download the text file, Apples.txt, available on Canvas. Define a Python function to read each type of apple in the text file, then print to the terminal the name and price of each apple. Honeycrisp, Empire, and Fuji apples sell for $2.00 each. All other apples sell for $1.00 each.
- Name the Python file: Ex7-ConditionalTests.py.
- Define a function named,
printApplePrices()and make sure it can read the data (i.e., the apples) in the Apples.txt file. - Use list comprehension and the strip() method to remove the new line characters.
- Define a for loop and write an if-else statement to print the cost of each apple to the terminal as described in the exercise description.
- Solution Output Example:
Fuji $2.00
Granny Smith $1.00
Pink Lady $1.00
Honeycrisp $2.00
Envy $1.00
Gala $1.00
Jazz $1.00
Cameo $1.00
Golden Delicious $1.00
Empire $2.00
McIntosh $1.00
Opal $1.00
Winesap $1.00
Pacific Rose $1.00
Ambrosia $1.00