Search code examples
pythoneasygui

How to check if user input has these lines?


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..


Solution

  • 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.