Nutrition Facts
Food Standards Australia and New Zealand offers an online database for over 1600 foods with detailed nutritional information.
In a file called nutrition.py, implement a program that prompts consumers users to input a fruit (case-insensitively) and then outputs the number of kilojoules in 100g of that fruit or vegetable, per the table below. Capitalisation aside, assume that users will input fruits exactly as written in the table (e.g., strawberries, not strawberry). Ignore any input that isn’t a fruit.
Hints
- Rather than use a conditional with 20 Boolean expressions, one for each fruit, better to use a
dictto associate a fruit with its calories! -
If
kis astranddis adict, you can check whetherkis a key indwith code like:if k in d: ... -
Take care to output the fruit’s calories, not calories from fat!
Data
| Fruit | Calories (Kilojoules per 100g) |
|---|---|
| Apple | 227 |
| Apricot | 167 |
| Avocado | 579 |
| Banana | 394 |
| Blackberries | 211 |
| Blueberries | 194 |
| Cherries | 250 |
| Feijoa | 176 |
| Leek | 136 |
| Mushroom | 87 |
| Onion | 139 |
| Peas | 294 |
| Capsicum | 115 |
| Potato | 272 |
| Pumpkin | 189 |
| Radish | 62 |
| Strawberries | 108 |
Before You Begin
From the root of your repository execute cd src/loops So your current working directory is ...
nutrition.py where you’ll write your program.
How to Test
Here’s how to test your code manually:
- Run your program with
python nutrition.py. TypeAppleand press Enter. Your program should output: - Run your program with
python nutrition.py. TypeAvocadoand press Enter. Your program should output: - Run your program with
python nutrition.py. TypeCherriesand press Enter. Your program should output -
Run your program with
python nutrition.py. TypeTomatoand press Enter. Your program should outputNo data available for this fruit. -
Be sure to try other fruits and vary the casing of your input. Your program should behave as expected, case-insensitively.
Pytest
You can execute the below to check your code using pytest from the root directory.
A green output from running the test means it was successful. A red output means there is a bug in your code that you need to fix.
How to Submit
From github desktop or the command line, commit your changes and push them to your repository.
Codespaces
If you are using codespaces, you can commit your changes directly from the Codespace interface. Click on the Source Control icon in the left sidebar, then click on the "..." button and select "Commit to main". Enter a commit message and click "Commit".
Codespace terminal or your local terminal.
Note
You will need to have installed git-scm for this to work locally
At the /datatypes $ prompt in your terminal:
Note
Remember to replace "your message here" with a meaningful commit message that describes your changes.