0

プログラムのコーディングに少し行き詰まっています。バイナリ入力ではないという私の例外は機能していないようです。プログラムを実行すると、「if False in [i == '0' or i == '1' for i in bin2dec]: TypeError: 'int' object is not iterable」というエラー メッセージが表示されます。誰かが助けることができれば。

e1=True
print"Welcome to CJ's Program V1.00.8\n"
    while e1:
        try:
            bininput= int(input("Please enter a binary number: "))
            e1=False
        except NameError: 
            print"Please try again.\n"
            time.sleep(0.5)
        except SyntaxError: 
            print"Please try again.\n"
            time.sleep(0.5)

    if False in [i == '0' or i == '1' for i in bininput]:
        print "\nIts not Binary number. Please try again."
        time.sleep(1)
    else:
        print "\nIts a Binary number!\n" 

        decnum = 0 
        for i in bininput: 
            decnum = decnum * 2 + int(i)
            time.sleep(0.25)
        print decnum, "<<This is your answer.\n"
4

1 に答える 1

0

人々が言っ​​たように、文字列とは異なり、整数の各数字/文字をループすることはできません-それが意味することです。

これに対する最も単純な解決策は、毎回 int を文字列に変換して文字列として反復処理することです。

e1=True
print"Welcome to CJ's Program V1.00.8\n"
while e1:
    try:
        bininput= int(input("Please enter a binary number: "))
        e1=False
    except NameError: 
        print"Please try again.\n"
        time.sleep(0.5)
    except SyntaxError: 
        print"Please try again.\n"
        time.sleep(0.5)

if False in [i == '0' or i == '1' for i in str(bininput)]:
    print "\nIts not Binary number. Please try again."
    time.sleep(1)
else:
    print "\nIts a Binary number!\n" 

    decnum = 0 
    for i in str(bininput): 
        decnum = decnum * 2 + int(i)
        #time.sleep(0.25)
    print decnum, "<<This is your answer.\n"
于 2013-09-15T17:01:10.270 に答える