2017-08-03 6 views
-1

反復可能ではありません「TypeError例外: 『タイプ』オブジェクトが反復可能ではない」が、私はかなりPythonの例外TypeError:「type」をオブジェクトが、私はこのエラーに他のスレッドを見てきました

a = ["Hey","Oh",32,12,"No",41] 
b = [23,65,2,7,21,29] 
c = ["My","Friends","At","Coding","Dojo"] 

def listType(arg): 
    new_string = "" 
    numSum = 0 

for value in type(a): 
    if isinstance(value,int) or isinstance(value,float): 
     numSum += value 
    elif isinstance(value,str): 
     new_string += value 
    if new_string and numSum: 
     print "String:", new_string 
     print "Sum:", numSum 
     print "This list is of mixed type" 
    elif new_string: 
     print "String:", new_string 
     print "This list is of string type" 
    else: 
     print "Sum:", numSum 
     print "This list is of integer type" 

print listType(a) 
間違っているものを理解していませんよ
+0

'(a)の値のために:'あなたはここで何をするのですか? –

+0

@ WillemVanOnsem-リストを取得し、その要素のデータ型に基づいて、リストの各要素のメッセージを に出力するプログラムを作成します。 プログラム入力は常にリストになります。リスト内の各項目について、 がそのデータ型をテストします。項目が文字列の場合は、新しい文字列に連結します。 数値の場合は、合計に加算します。あなたのプログラムの終わりに、 文字列、数字、およびリストに含まれているものの分析を印刷します。 1つのタイプが の場合はそのタイプを、それ以外の場合は 'mixed' –

答えて

0

type(a)を照会すると、listが得られます。おそらく、マッピング対応型への要素のをしたいので、map使用:あなたはlistTypeのないprint結果は、それがないreturn何もしないし、以来、修正する必要がありますさらに

def listType(a): 
    new_string = "" 
    numSum = 0 
    for value in map(type,a): 
     if isinstance(value,int) or isinstance(value,float): 
      numSum += value 
     elif isinstance(value,str): 
      new_string += value 

    if new_string and numSum: 
     print "String:", new_string 
     print "Sum:", numSum 
     print "This list is of mixed type" 
    elif new_string: 
     print "String:", new_string 
     print "This list is of string type" 
    else: 
     print "Sum:", numSum 
     print "This list is of integer type" 

listType(a)

プログラムのインデント。私はそれが今正しいことを願っています。

関連する問題