意外的缩进错误 - 试图调整别人的代码

时间:2016-07-17 01:51:50

标签: python python-2.7

这里的代码。我不知道python并试图调整别人的代码

我正在尝试编写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()

3 个答案:

答案 0 :(得分:0)

在Python中,没有括号来定义块。它是用空格完成的。因此,如果您的行具有与其他行不同的缩进量,或者您缺少应该有某些缩进的缩进,则解释器将引发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()

答案 1 :(得分:0)

你必须改变开始 - 它有for语句的位置。在python中,最后需要冒号的东西必须缩进。

最简单的方法是遍历for语句中的每一行,然后按TAB键。这将为您缩进代码。希望它有所帮助!

我也会观看或阅读python的基本教程,其中展示了缩进的工作原理。

答案 2 :(得分:0)

好的,所以我希望你假设你的代码比我们所有人的代码都多(实际上很难在没有更多代码的情况下回答这些问题),那么如果你已经宣布了所有的变量什么不在幕后,那么你应该做的就是像这样修复缩进:

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

如果你仍然是python的新手,那么可能会给this一个阅读,如果你仍然不确定,那么继续给this一个读。我发现两者都非常有帮助。

相关问题