Python Forum

Full Version: not not working looping through a dictionary
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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 . . .
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()))
That did it. Thanks so much!