2017-12-25 9 views
0

として解釈することができない私は、コードのこの部分を持っている:Pythonの例外TypeError:「strの」オブジェクトが整数

  if current_ins[0] == "REPEAT": 
       for i in range(current_ins[1]): 
        if last_ins != "": 
         instructions.append(last_ins) 
         if delay != -1: 
          instructions.append(["DELAY", delay]) 
        else: 
         print ("ERROR: REPEAT can't be the first instruction") 
         sys.exit(-1) 

と、残念ながら私はこのエラーを取得する:

Duck Encoder 0.1.1 by Roger Serentill & GoldraK 
Traceback (most recent call last): 
    File "D:\devloc\Encoders-decoders\USB-Rubber-Ducky-master\Encoder\Encoder.py", line 379, in <module> 
    p.compile(sys.argv) 
    File "D:\devloc\Encoders-decoders\USB-Rubber-Ducky-master\Encoder\Encoder.py", line 56, in compile 
    instructions = self.__read_file() 
    File "D:\devloc\Encoders-decoders\USB-Rubber-Ducky-master\Encoder\Encoder.py", line 263, in __read_file 
    for i in range(current_ins[1]): 
TypeError: 'str' object cannot be interpreted as an integer 

私は何ができますか?

私はPython3を使用しています。

+1

'current_insは、[1]'は何が含まれていない - :あなたが興味を持っている場合

は、見てみましょうか?可能であれば、数値を含む文字列になる可能性があり、変換する必要があります: 'range(int(current_ins [1]))' – Elisha

+0

ありがとう! range(int(current_ins [1]))は完全に機能します! – devlime26

答えて

0

range(len(current_ins[1])):またはrange(int(current_ins[1])):を試してください。それはcurrent_ins [1]の中に何があるかによって異なります。

+0

ありがとうございました! range(int(current_ins [1])):完全に機能します! – devlime26

0

は、私はあなたがこのような何かを解析しようとしているとします -

Inst1 
Inst2 
REPEAT 5 

今、あなたは「数」あなたが繰り返しで指定されているものは何でも、前の手順を繰り返すようにしようとしています。

あなたは確かにそれをintに変換することができます。 int(currenct_inst[1])しかし、それは本当にあいまいです。あなたはREPEAT (::)のようなinstrucitonを処理したい場合、私はおそらく

if current_instruction[0] = repeat: 
    # strip here removes the leading and trailing whitespace 
    times_repeat = int(current_instrucitons[1].strip()) 

、より明示的なことをアドバイスzen of pythonによると、あなたは例外hadnlingを見てみる必要があります。
https://www.programiz.com/python-programming/exception-handling

関連する問題