Exercise 6-6 Solution Python Crash Course Chapter 6: Dictionaries

 

πŸ“˜ Python Crash Course – Chapter 6: Dictionaries – Exercise 6-6 Solution

Welcome to another solution from Python Crash Course by Eric Matthes.


πŸ“ Exercise 6-6

Task:
• Make a list of people who should take the favorite languages poll . Include some names that are already in the dictionary and some that are not .
• Loop through the list of people who should take the poll . If they have already taken the poll, print a message thanking them for responding .
If they have not yet taken the poll, print a message inviting them to take the poll

✅ Solution Code:


people = [ "Sam" , "Tommy" , "Ram" , "Aman" , "Robert", "Akash"]

fav_lang ={
    'Sam': "English",
    "Ram" : "Hindi",
    "Aman" : "French",
    "Robert": "English"
}


participant =[]

for name in fav_lang.keys():
    participant.append(name)

for i in people:
    if i in participant:
        print(f"Thankyou {i} For Participating. You Choose {fav_lang[i]} as your Favourite Language")
    else:
        print(f'Sorry {i}. Please Participate In The Pole ')

🧠 Code Explanation:

This Python code demonstrates how to check if people have participated in a poll and display appropriate messages based on their participation. Here's how it works:

  1. Create a list of people:

    The variable people is assigned a list of names representing individuals who should take the poll. Some of these names are already in the dictionary, while others are not. For example:

    • "Sam"
    • "Tommy"
    • "Ram"
    • "Aman"
    • "Robert"
    • "Akash"
  2. Create a dictionary of participants:

    The variable fav_lang is assigned a dictionary where the keys are names of people who have already participated in the poll, and the values are their favorite languages. For example:

    • 'Sam': "English"
    • 'Ram': "Hindi"
    • 'Aman': "French"
    • 'Robert': "English"
  3. Extract participants:

    A for loop iterates through the keys of the fav_lang dictionary to create a list of participants who have already taken the poll. This list is stored in the variable participant.

  4. Check participation:

    A for loop iterates through the people list. For each person:

    • If the person is in the participant list, a message is displayed thanking them for participating and showing their favorite language.
    • If the person is not in the participant list, a message is displayed inviting them to take the poll.

This code demonstrates how to use dictionaries and lists to manage and check participation in a poll, and how to display customized messages based on the results.

πŸ” Output:


Thankyou Sam For Participating. You Choose English as your Favourite Language
Sorry Tommy. Please Participate In The Pole
Thankyou Ram For Participating. You Choose Hindi as your Favourite Language
Thankyou Aman For Participating. You Choose French as your Favourite Language
Thankyou Robert For Participating. You Choose English as your Favourite Language
Sorry Akash. Please Participate In The Pole

πŸ“š Related Exercises from Chapter 6:


πŸ”— Connect With Us:

“In the world of code, Python is the language of simplicity, where logic meets creativity, and every line brings us closer to our goals.”
— Only Python

πŸ“Œ Tags: #Python #PythonCrashCourse #CodingForBeginners #LearnPython #PythonProjects #PythonTips

“In the world of code, Python is the language of simplicity, where logic meets creativity, and every line brings us closer to our goals.”— Only Python

πŸ“Œ Follow Us And Stay Updated For Daily Updates

Comments