π 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:
-
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"
-
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"
-
Extract participants:
A
for
loop iterates through the keys of thefav_lang
dictionary to create a list of participants who have already taken the poll. This list is stored in the variableparticipant
. -
Check participation:
A
for
loop iterates through thepeople
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.
- If the person is in the
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:
- ➤ Exercise 6-1 | Person
- ➤ Exercise 6-2 | Favorite Numbers
- ➤ Exercise 6-3 | Glossary
- ➤ Exercise 6-4 | Glossary 2
- ➤ Exercise 6-5 | Rivers
- ➤ Exercise 6-6 | Polling
- ➤ Exercise 6-7 | People
- ➤ Exercise 6-8 | Pets
- ➤ Exercise 6-9 | Favorite Places
- ➤ Exercise 6-10 | Favorite Numbers
- ➤ Exercise 6-11 | Cities
- ➤ Exercise 6-12 | Extensions
π 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
πTrending Topics
π 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
π Follow Us And Stay Updated For Daily Updates
Comments
Post a Comment