Python Forum
not not working looping through a dictionary
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
not not working looping through a dictionary
#1
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 . . .
Reply
#2
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()))
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
That did it. Thanks so much!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  looping a dictionary harmans14 1 662 Apr-11-2021, 08:48 PM
Last Post: bowlofred
  making list in looping a dictionary glennford49 9 1,509 Jun-25-2020, 03:23 PM
Last Post: ndc85430
  Looping through dictionary and comparing values with elements of a separate list. Mr_Keystrokes 5 1,957 Jun-22-2018, 03:08 PM
Last Post: wavic
  Looping issue, stops working JohnOsis 7 2,339 Apr-05-2018, 09:20 PM
Last Post: nilamo
  Changing dictionary values not working SnekLover123 5 2,380 Nov-01-2017, 05:39 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020