Question

1) We have returned to our ingredients dictionary. We want to display the ingredients in alphabetical...

1) We have returned to our ingredients dictionary. We want to display the ingredients in alphabetical order. Dictionaries cannot be sorted, so we need to use the items method to convert the dictionary into a list of tuples. Then we can sort that new list of tuples, then display the ingredients.

  1. Use items and list to convert ingredients into a list of tuples.
  2. Sort the new list, called ingred_list
  3. Write a for loop which goes through each tuple in ingred_list.
  4. Use the print statement to display the ingredient and its quantity. Remember! Each element in the ingred_list is a tuple. You will need to use indices to access the elements within the tuple.

When you do these steps, you should match the output under Desired Output.

# Assign ingredients to a dictionary of
# food ingredients
ingredients = {"eggs":1, "flour":"1 cup", "sugar":"1 cup", "cocoa":"0.5 cups"}

# Use items() and list() to convert
# the dictionary /ingredients/
# into a list of tuples
ingred_list =

# Sort the list /ingred_list/


# Use a for Statement to display
# ingredients in /ingred_list/


desired output:

cocoa 0.5 cups
eggs 1
flour 1 cup
sugar 1 cup

2)

We have a very simple recipe for a glass of water. Use a for loop and a print statement to display the ingredients using key/value pairs.

Note: do not just write print("water 1 cup")!

When you are finished, you should match the output under Desired Output.

# Assign ingredients
ingredients = {"water":"1 cup"}

# Use a /for/ loop and /print/ statement
# to display the ingredients:


desired output:

water 1 cup

3)

We want to see how much flour we need and whether this recipe contains butter.

  1. Complete the print statement so the appropriate dictionary entry for flour is displayed.
  2. Complete the if conditional statement using the in operator to see whether butter is an ingredient in this recipe

When you are completed, your output should match the Desired Output.

# Assign ingredients to a dictionary of
# food ingredients
ingredients = {"eggs":1, "flour":"1 cup", "sugar":"1 cup", "cocoa":"0.5 cups"}

# Display the /flour/ quantity
print("Flour:")

# Check if this recipe takes butter:
if :
    print("This recipe takes butter!")
else:
    print("No butter in this recipe!")

desired output:

Flour: 1 cup
No butter in this recipe!
0 0
Add a comment Improve this question Transcribed image text
Answer #1

 
# Assign ingredients to a dictionary of
# food ingredients
ingredients = {"eggs":1, "flour":"1 cup", "sugar":"1 cup", "cocoa":"0.5 cups"}

# Use items() and list() to convert
# the dictionary /ingredients/
# into a list of tuples
ingred_list = list(ingredients.items())

# Sort the list /ingred_list/
ingred_list.sort()


# Use a for Statement to display
# ingredients in /ingred_list/
for (i, q) in ingred_list:
        print(i, ':', q)

#####################################################
#####################################################

# Assign ingredients
ingredients = {"water":"1 cup"}

# Use a /for/ loop and /print/ statement
# to display the ingredients:
for (i, q) in ingredients.items():
        print(i, q)

#####################################################
#####################################################

# Assign ingredients to a dictionary of
# food ingredients
ingredients = {"eggs":1, "flour":"1 cup", "sugar":"1 cup", "cocoa":"0.5 cups"}

# Display the /flour/ quantity
print("Flour:", ingredients['flour'])

# Check if this recipe takes butter:
if 'butter' in ingredients:
    print("This recipe takes butter!")
else:
    print("No butter in this recipe!")


Please upvote, as i have given the exact answer as asked in question. Still in case of any issues in code, let me know in comments. Thanks!

Add a comment
Know the answer?
Add Answer to:
1) We have returned to our ingredients dictionary. We want to display the ingredients in alphabetical...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • A cookie recipe calls for the following ingredients: • 1.5 cups of sugar • 1 cup...

    A cookie recipe calls for the following ingredients: • 1.5 cups of sugar • 1 cup of butter • 2.75 cups of flour The recipe produces 48 cookies with this amount of ingredients. Write a program that asks the user how many cookies they want to make and then displays the number of cups of each ingredient needed for the specified number of cookies in the following format: You need 5 cups of sugar, 3 cups of butter, and 7...

  • Homework P Do Homework - BreAnna ON C mathxl.com Homework: HW 6.4 Score: 0 of 1...

    Homework P Do Homework - BreAnna ON C mathxl.com Homework: HW 6.4 Score: 0 of 1 pt 6.4.42 1 The list of ingredients for chocolate brownies given at right will make 16 brownies. Use the list to decide how much of each ingredient is needed to make 12 brownies cup butter 2 ounces unsweetened chocolate 1 cups sugar teaspoons vanilla 2 1 2 eggs 3 cup flour cup(s) of butter is needed. (Simplify your answer. Type an integer, simplified fraction,...

  • Questions 1. How to create a comment in python? 2. The way to obtain user input...

    Questions 1. How to create a comment in python? 2. The way to obtain user input from command line 3. List standard mathematical operators in python and explain them 4. List comparison operators in python 5. Explain while loop. Give example 6. Explain for loop. Give example 7. How to create infinite loop? And how to stop it? 8. Explain a built-in function ‘range’ for ‘for’ loop 9. Explain break statement 10. Explain continue statement 11. Explain pass statement 12....

  • Python please For this program, we will be making lists for the local Zoo. You have...

    Python please For this program, we will be making lists for the local Zoo. You have been provided with 4 lists of data already created for you. Using this data, we update the data at this Zoo. YOU MUST USE LIST FUNCTIONS FOR REMOVING TO RECEIVE YOUR FULL POINTS! Input: A type of animal -- This information should be provided in response the question: "What animal are we removing?" A number corresponding to the selection of a menu number (see...

  • In PYTHON ONLY PLEASE You will use a dictionary in Python You only have string in Python so you have to use a string dat...

    In PYTHON ONLY PLEASE You will use a dictionary in Python You only have string in Python so you have to use a string data type Use a Switch statement.A switch statement is just another way of writing an IF statement. The user will enter f, s, j, or r from the keyboard for freshman, sophomore, junior, or senior. Use character data type for c++. Then you can say something like: char level ='f'; level=toupper(level); You would need a loop...

  • Homework - 4 Name: 1- Intake Analysis Carbohydrates Eating Plan B (1 Day's Intake) 6 ounces...

    Homework - 4 Name: 1- Intake Analysis Carbohydrates Eating Plan B (1 Day's Intake) 6 ounces grapefruit juice 2 scrambled eges 1 ounce cheddar cheese 20 ounces coffee 2 ounces soy milk 1 cup fresh raspberries 1 cup cantaloupe 1 honey oat granola bar 1 cup vanilla yogurt 6 ounces grilled salmon 10 cooked asparagus spears 1 cup broccoli 4 ounces white wine 4 ounces blueberry juice + seltzer water 20 barbecue flavor soy crisps 1 cup wasabi peas 13"...

  • Could anyone help add to my python code? I now need to calculate the mean and...

    Could anyone help add to my python code? I now need to calculate the mean and median. In this programming assignment you are to extend the program you wrote for Number Stats to determine the median and mode of the numbers read from the file. You are to create a program called numstat2.py that reads a series of integer numbers from a file and determines and displays the following: The name of the file. The sum of the numbers. The...

  • (Write or type in answers, except for problem #15, which should be entered as a small...

    (Write or type in answers, except for problem #15, which should be entered as a small program, tested, and submitted in Repl.it) 1. Write a Python statement to define a list named temps using the following elements, in order: 95, 100, 77, 54, 103, 82 2. a) What is the length of the list temps? b) What Python function can be used to obtain the length of the list? Use it in a statement to obtain the length of temps...

  • Suppose that we want to help physicians to diagnose illnesses. A physician observes a patient's symptoms and considers the illnesses that could be associated with those symptoms. Design and implem...

    Suppose that we want to help physicians to diagnose illnesses. A physician observes a patient's symptoms and considers the illnesses that could be associated with those symptoms. Design and implement a class PhysiciansHelper thatprovide a list of those illnesses. PhysiciansHelper should contain a dictionary of illnesses and symptoms. A method should read a text file of illnesses and their symptoms into the dictionary. See format in sample data file. PhysiciansHelper should read a list of symptoms for a patient. A...

  • Python program This assignment requires you to write a single large program. I have broken it...

    Python program This assignment requires you to write a single large program. I have broken it into two parts below as a suggestion for how to approach writing the code. Please turn in one program file. Sentiment Analysis is a Big Data problem which seeks to determine the general attitude of a writer given some text they have written. For instance, we would like to have a program that could look at the text "The film was a breath of...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT