Python Forum
not not working looping through a dictionary - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: not not working looping through a dictionary (/thread-13217.html)



not not working looping through a dictionary - rtbr17 - Oct-04-2018

peoples = ['jen', 'sarah', 'edward', 'phil', 'jeff', 'cara', 'ben', 'adam', 'larry']
favorite_languages = {
    'jen' : 'python',
    'sarah' : 'c',
    'edward' : 'ruby',
    'phil' : 'python',
    'jeff' : 'python',
    'cara' : 'none',
    }
for people in favorite_languages.keys():
	if people in favorite_languages:
		print("Thanks " + people.title() + " for taking the survey.")
	if people not in favorite_languages:
		print(people + ", Please take the survey.")
Output when run. The not gets overlooked.

Thanks Jen for taking the survey.
Thanks Sarah for taking the survey.
Thanks Edward for taking the survey.
Thanks Phil for taking the survey.
Thanks Jeff for taking the survey.
Thanks Cara for taking the survey.


------------------
(program exited with code: 0)

Press any key to continue . . .


RE: not not working looping through a dictionary - buran - Oct-04-2018

on line 10 you want to iterate over the list peoples, not over the keys of the dict.

peoples = ['jen', 'sarah', 'edward', 'phil', 'jeff', 'cara', 'ben', 'adam', 'larry']
favorite_languages = {
    'jen' : 'python',
    'sarah' : 'c',
    'edward' : 'ruby',
    'phil' : 'python',
    'jeff' : 'python',
    'cara' : 'none',
    }
for person in peoples:
    if person in favorite_languages:
        print("Thanks {} for taking the survey.".format(person.title()))
    else:
        print("{}, Please take the survey.".format(person.title()))



RE: not not working looping through a dictionary - rtbr17 - Oct-04-2018

That did it. Thanks so much!