1

It's my second program in Python. I seem to be happy with my progress. The question is I am trying to make is:

choice=eg.ynbox(msg='wat to do')
for["true","1"] in choice:
        print("yes")
for["false","0"] in choice:
        print("no")

The problem is that this for condition is not working. I have seen such code while I was looking up answers for my previous question but I forget. I tried googling but I dont know how to put this in words..A little syntax help is necessary BTW: its a gui program with easygui 0.96..

Squazic
  • 3,670
  • 3
  • 26
  • 40
Surya Teja Karra
  • 541
  • 1
  • 6
  • 19
  • 2
    I don't understand what you want the code to do. – Karl Knechtel Nov 29 '12 at 10:25
  • actually its a test program for assisting my friend. I just wanted to make sure that this 'part of code' works in his program. Its that he is doing a gui program and got stuck so i learned basics of easygui and was helping him wherein I got stuck at this part :P-@KarlKnechtel – Surya Teja Karra Nov 29 '12 at 10:47
  • How do you want anybody to know what to do if you don't inform about the values that **choice** is likely to take ? We aren't all likely to know **easygui** – eyquem Nov 29 '12 at 11:49
  • oh sorry..@eyquem choice just opes a yes/no dialog box. i was in middle of my friends software which is gui... as u can see in the above code, for condition isn't working... when the yes/no buttons are pressed, it returns true/false for python 3.x and 1/0 for python 2.x... – Surya Teja Karra Nov 29 '12 at 13:53

3 Answers3

1
choice = eg.ynbox(msg='wat to do')
if any(word in choice for word in ["true","1"]):
    print('yes')
elif any(word in choice for word in ["false","0"]):
    print('no')
else:
    print('invalid input')

or, if the list is short:

choice = eg.ynbox(msg='wat to do')
if 'true' in choice or '1' in choice::
    print('yes')
if 'false' in choice or '0' in choice::
    print('no')
else:
    print('invalid input')
eumiro
  • 207,213
  • 34
  • 299
  • 261
  • I tried this code but it game me this: Traceback (most recent call last): File "E:\misc\1.py", line 7, in if any(word in choice for word in ["true","1"]): File "E:\misc\1.py", line 7, in if any(word in choice for word in ["true","1"]): TypeError: argument of type 'int' is not iterable @eumiro – Surya Teja Karra Nov 29 '12 at 10:49
0

I assume by eg.ynbox(msg='wat to do') you mean you are creating a Yes/No dialog box. This means that the value stored in choice is an Integer where 1 represents True and 0 represents False. This is correct in both Python 2.x and Python 3.x as long as True and False have not been reassigned in Python 2.x True and False are reserved keywords in Python 3.x and thus are guaranteed not to change. Therefore, you simply have an if statement that works using this value:

if choice:
    print 'Yes'
else:
    print 'No'

You do not need to match on 1 and 0 since they represent both True and False.

BeRecursive
  • 6,286
  • 1
  • 24
  • 41
  • ya but new versions of Python represent Yes and No as True and False. My program isn't gonna work if this occcurs. – Surya Teja Karra Nov 29 '12 at 10:51
  • I don't understand why you'd be running on multiple versions of python? Surely you run one version of python and you don't keep switching between 2.x and 3.x? – BeRecursive Nov 29 '12 at 10:55
  • yes but the program which my friend is creating is to be made ready to run in any python version my pc is windows and python 3.x but my friend is python 2.x ubuntu..so i need this code to work in both versions... besides the list is not these 2. as i mentioned earlier, this is a test program... – Surya Teja Karra Nov 29 '12 at 11:14
  • Yes but this just seems redundant. There are many more issues you are going to come up against in the difference between 2.x and 3.x. I suggest you stick to a version as developing like this is a poor plan. – BeRecursive Nov 29 '12 at 11:16
  • @SuryaTejaKarra OK I clarified and fixed your issue – BeRecursive Nov 29 '12 at 14:08
0

You might try the following code in place of yours:

def is_accept(word):
    return word.lower() in {'true', '1', 'yes', 'accept'}

def is_cancel(word):
    return word.lower() in {'false', '0', 'no', 'cancel'}

def get_choice(prompt=''):
    while True:
        choice = eg.ynbox(msg=prompt)
        if is_accept(choice):
            print('Yes')
            return True
        if is_cancel(choice):
            print('No')
            return False
        print('I did not understand your choice.')
Noctis Skytower
  • 21,433
  • 16
  • 79
  • 117