Python EasyGUI multchoicebox - 对多个项目的测验 IF 语句不起作用

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

我想要一个基于正确选择的 easyGUI 多选框显示输出。以下不起作用(即不显示 '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":
if choice == fieldnames[1] and fieldnames[2]:

相同
if (choice == fieldnames[1]) and fieldnames[2]:

这意味着它将检查 choice 是否等于 fieldnames[1] 以及 fieldnames[2] 是否等于 "truthy"。 (如果你 select "Correct",应该是 True)。

您可能想要检查的是:

if choice in ( fieldnames[1], fieldnames[2] ):

您可以使用 "in" 运算符来检查用户是否选择了所需的字段。

然后,为了将逻辑应用于两个所选值,只需添加 "and"。

这是经过更正和测试的代码:

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')