1

I seem to be having some issues with my if/else statements and EasyGUI choice box. I know that if the user presses "Cancel", it returns "None". My code always enters the "Else" statement. Also, my second For loop doesn't seem to be iterating. Is there a better way to select the object (country name) that I want? Here's my code so far, with debugging output.

UPDATE: The issue was the fact that I was using a Mac for some reason. I used my PC, and it worked as expected.

  • Are you sure it returns the string `'None'` and not `None`? Have you tried `if choice == None:` or `if choice is None:`? – tobias_k Feb 23 '14 at 19:39
  • choice != 'None, choice = None! str vs builtin None. – cox Feb 23 '14 at 19:40
  • About the second loop: Seems like your `csvdict` iterator is exhausted after the first loop. Try creating a new iterator or copy the contents to a list if it's not too much. – tobias_k Feb 23 '14 at 19:41

1 Answers1

1

According to this, choicebox returns None then the cancel button is pressed, and not the string 'None'. Change your condition to if choice is None: and it should work.

The problem with your second for loop is that csvdict is an iterator which is exhausted after you use it in your first for loop. You could either create a new iterator, or store the contents of the file in a list, or dictionary, if it is not too much data. You already did this for the country field, but you seem to need all the fields. The best might be to create a dict mapping the country names to lists holding the other fields, so you can quickly access them instead of having to loop over all the contries again.

// instead of your first for loop
choices = dict()
for line in csvdict:
    choices[line['Country']] = line
...
// instead of your second for loop
if choice in choices:
    line = choices[choice]
    ...
tobias_k
  • 81,265
  • 12
  • 120
  • 179
  • Thanks for your reply. The first part of your code works perfectly. I'm having a different issue with your second bit of code. When it runs, it says: line = choices[choice] TypeError: list indices must be integers, not str. Do you have any idea why that might be? –  Feb 23 '14 at 20:07
  • Choice is the name of the country the user selected in the choice box. –  Feb 23 '14 at 20:12
  • Oh, right, now I see it. Are you sure you changed to first for loop so it creates a dictionary mapping country names to the rest of the information? It seems like `choices` is still a plain old list in your case. – tobias_k Feb 23 '14 at 20:13
  • Oh, I see where you changed that now. I'm getting this error for that code: choices[line['Country']] = line TypeError: list indices must be integers, not str –  Feb 23 '14 at 20:16
  • Did you change the line above the loop to make `choices` a dictionary? `choices = dict()` And just in case you are getting this as your next error, change `choicebox(msg, title, choices)` to `choicebox(msg, title, list(choices))` – tobias_k Feb 23 '14 at 20:18
  • Nice! Thanks for this.`if choice is None:` worked for me. – MelanieLauzon May 20 '20 at 23:17