0

When iterating through the list, users, I want to print the only the names of the users that are in a dictionary to congratulate them, then reuse the list to print users that have not completed the poll. Why after the 1st for loop of users, does the list gets changed, or how do I reset it as I thought the list was not being modified. The only way I can get this program to work is to make a 2nd list in the beginning of the program called testusers.

users = ['jen','seymor', 'patrick', 'tony', 'sarah','edward','billie','mark','jan']  
testusers = users
favorite_languages = {
    'jen': 'python',
    'sarah': 'c',
    'edward': 'ruby',
    'phil': 'python',
    'jan': 'python',
    'mark': 'java',
    }  

print '\nThe following users have taken the poll\n'
for users in favorite_languages.keys():
    print('\t'+users.title() + ", thank you for talking the poll.")

print '\nThe following "PERSONS" have not taken the poll.\n'      
for ditcher in users:  
  if ditcher not in favorite_languages.keys(): 
      print "Hey "+ ditcher.title()+ " take the  poll!"  
JSerra1
  • 1
  • 1

2 Answers2

3
users = ['jen', 'seymor', 'patrick', 'tony', 'sarah', 'edward', 'billie', 'mark', 'jan']
...
for users in favorite_languages.keys():

Only one thing can be called users at a time. Call the second variable user.

Alex Hall
  • 34,833
  • 5
  • 57
  • 89
2

You should not use for users in favorite_languages.keys():, by doing so, you are overriding the original value of users (your list).

At the end of your for loop, users takes the last value in your dictionary (which could be any key, because dictionary is not ordered):

>>> users = ['jen','seymor', 'patrick', 'tony', 'sarah','edward','billie','mark','jan']
>>> for users in favorite_languages.keys():
...     print users
...
sarah
phil
mark
jan
edward
jen  # <=== users takes the last printed value
>>> users
'jen'

Instead, choose another name for your loop variable:

for u in favorite_languages.keys():
   print('\t'+u.title() + ", thank you for talking the poll.")
ettanany
  • 19,038
  • 9
  • 47
  • 63