Search code examples
pythoneasygui

Python EasyGUI multchoicebox - Quiz IF statement on multiple items not working


I would like to have an easyGUI multchoicebox display output based on the correct choices being made. The following doesn't work (i.e it doesn't show 'correct')

import easygui

fieldnames = ["Incorrect", "Correct", "Also Correct"]
choice = easygui.multchoicebox("Pick an option.", "", fieldnames)
if choice == fieldnames[1] and fieldnames[2]:
    easygui.msgbox('Correct!')
else:
    easygui.msgbox('Incorrect')

#Also tried:
#if choice == "Correct" and "Also Correct":

Solution

  • You can use the "in" operator to check if the desired field was selected by the user.

    Then, in order to apply the logic to both chosen values, simply add "and".

    Here's the code, corrected and tested:

    import easygui
    
    fieldnames = ["Incorrect", "Correct", "Also Correct"]
    choice = easygui.multchoicebox("Pick an option.", "", fieldnames)
    
    if fieldnames[1] in choice and fieldnames[2] in choice:
        easygui.msgbox('Correct!')
    else:
        easygui.msgbox('Incorrect')