1

これはPythonでの私の2番目のプログラムです。私は自分の進歩に満足しているようです。問題は私が作ろうとしていることです:

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

問題は、これが条件に対して機能していないことです。前の質問の答えを探しているときにそのようなコードを見たことがありますが、忘れています。グーグルを試しましたが、これを言葉で表現する方法がわかりません。少し構文の助けが必要です。ところで、easygui0.96を使用したGUIプログラムです。

4

3 に答える 3

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

または、リストが短い場合:

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')
于 2012-11-29T10:30:27.070 に答える
0

代わりに次のコードを試すことができます。

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.')
于 2012-11-29T14:54:55.717 に答える
0

はい/いいえダイアログボックスeg.ynbox(msg='wat to do')を作成していると思います。これは、に格納されている値がであるということを意味します。ここで、1Trueを表し、0Falseを表します。これは、Python2.xでTrueFalseが再割り当てされておらず、Python 3.xで予約済みのキーワードであり、変更されないことが保証されている限り、 Python2.xとPython3.xの両方で正しいです。したがって、次の値を使用して機能するステートメントがあります。choiceInteger TrueFalseif

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

とを表すので、1とを一致させる必要はありません。0TrueFalse

于 2012-11-29T10:30:29.213 に答える