2016-07-17 6 views
-3

ここにコードです。私のpythonを知っていると私はブロックを定義するには何の括弧がない、Pythonではループ予期しないインデントエラー - 誰か他のコードを微調整しようとしています

 for poke in visible: 
     other = LatLng.from_degrees(poke.Latitude, poke.Longitude) 
     diff = other - origin 
     # print(diff) 
     difflat = diff.lat().degrees 
     difflng = diff.lng().degrees 
     direction = (('N' if difflat >= 0 else 'S') if abs(difflat) > 1e-4 else '') + (('E' if difflng >= 0 else 'W') if abs(difflng) > 1e-4 else '') 

     print("(%s) %s is visible at (%s, %s) for %s seconds (%sm %s from you)" % (poke.pokemon.PokemonId, pokemons[poke.pokemon.PokemonId - 1]['Name'], poke.Latitude, poke.Longitude, poke.TimeTillHiddenMs/1000, int(origin.get_distance(other).radians * 6366468.241830914), direction)) 

    with open("test.txt", "a") as myfile:  
     myfile.write("(%s) %s is visible at (%s, %s) for %s seconds (%sm %s from you)" % (poke.pokemon.PokemonId, pokemons[poke.pokemon.PokemonId - 1]['Name'], poke.Latitude, poke.Longitude, poke.TimeTillHiddenMs/1000, int(origin.get_distance(other).radians * 6366468.241830914), direction)) 
     myfile.write("\n") 
     myfile.flush() 
+0

コードは実際にどのように見えますか? – IanAuld

+0

これはほんの一部です。質問は簡単です。 – MonsterMMORPG

+0

最初のforループはインデントしないでください。また、その本体に追加のインデントが必要です。文脈がなければ、伝えるのは不可能です。トレースバックを含む完全なエラーメッセージを追加する必要があります。 –

答えて

0

のために印刷されたものを記述しようとしています誰か他の人のコード

を微調整しようとしていません。これは空白で行われます。したがって、他の行とは異なるインデント量の行がある場合、またはある行のインデントがない場合、インタプリタはIndentationErrorを発生させます。

コードが正しく貼り付けられていると仮定すると、適切にインデントされていないため、その下にコンテンツのないforループがあります。これは次のようになります。

for poke in visible: 
    other = LatLng.from_degrees(poke.Latitude, poke.Longitude) 
    diff = other - origin 
    # print(diff) 
    difflat = diff.lat().degrees 
    difflng = diff.lng().degrees 
    direction = (('N' if difflat >= 0 else 'S') if abs(difflat) > 1e-4 else '') + (('E' if difflng >= 0 else 'W') if abs(difflng) > 1e-4 else '') 

    print("(%s) %s is visible at (%s, %s) for %s seconds (%sm %s from you)" % (poke.pokemon.PokemonId, pokemons[poke.pokemon.PokemonId - 1]['Name'], poke.Latitude, poke.Longitude, poke.TimeTillHiddenMs/1000, int(origin.get_distance(other).radians * 6366468.241830914), direction)) 

with open("test.txt", "a") as myfile:  
    myfile.write("(%s) %s is visible at (%s, %s) for %s seconds (%sm %s from you)" % (poke.pokemon.PokemonId, pokemons[poke.pokemon.PokemonId - 1]['Name'], poke.Latitude, poke.Longitude, poke.TimeTillHiddenMs/1000, int(origin.get_distance(other).radians * 6366468.241830914), direction)) 
    myfile.write("\n") 
    myfile.flush() 
0

ここでは、for文がある場所を変更する必要があります。 Pythonでは、最後にコロンを必要とするものはインデントしなければなりません(MUST)。

最も簡単な方法は、forステートメントの各行を調べ、Tabキーを押すことです。これによりコードがインデントされます。それが役に立てば幸い!

idまた、インデントの仕組みを示すpythonの基本チュートリアルを見たり読んだりしてください。

0

あなたが私たちの皆さんに見せたものよりも多くのコードを持っていると思われます(実際にはこれらの質問に答えることは時折ありません)。変数や舞台裏のものではありません、あなたがしなければならない必要があるすべてがそうのようにインデントを修正している:you'rはまだのpythonにはかなり新しい、おそらくthisに読み取りを与えるに行く場合は、あなたがしている場合

for poke in visible: 
    other = LatLng.from_degrees(poke.Latitude, poke.Longitude) 
    diff = other - origin 
    # print(diff) 
    difflat = diff.lat().degrees 
    difflng = diff.lng().degrees 
    direction = (('N' if difflat >= 0 else 'S') if abs(difflat) > 1e-4  
    else '') + (('E' if difflng >= 0 else 'W') if abs(difflng) > 1e-4 
    else '') 
    print("(%s) %s is visible at (%s, %s) for %s seconds (%sm %s from 
    you)" % (poke.pokemon.PokemonId, pokemons[poke.pokemon.PokemonId - 
    1]['Name'], poke.Latitude, poke.Longitude, poke.TimeTillHiddenMs/
    1000, int(origin.get_distance(other).radians * 6366468.241830914), 
    direction)) 

    with open("test.txt", "a") as myfile:  
     myfile.write("(%s) %s is visible at (%s, %s) for %s seconds  
     (%sm %s from you)" % (poke.pokemon.PokemonId, 
     pokemons[poke.pokemon.PokemonId - 1]['Name'], poke.Latitude, 
     poke.Longitude, poke.TimeTillHiddenMs/1000, 
     int(origin.get_distance(other).radians * 6366468.241830914), 
     direction)) 
     myfile.write("\n") 
     myfile.flush() 

それでもまだ分かりませんが、thisも読んでください。私はどちらもかなり役に立ちました。

関連する問題