我在這裏要做的是寫一個文本文件,如果它還沒有存在的小寵物的發現的經緯度。由於我使用的是無限循環,因此我添加了一個if-state來防止已經存在的一對座標被添加。 請注意,我也有一個存儲相同信息的列表座標。該列表的工作原理是不添加重複(通過檢查)但是,文本文件具有相同的座標,即使它理論上不應該如同包含在與列表相同的if塊中。If語句看起來被寫操作忽略
import requests
pokemon_url = 'https://pogo.appx.hk/top'
while True:
response = requests.get(pokemon_url)
response.raise_for_status()
pokemon = response.json()[0:]
Sighting = 0
Coordinates = [None] * 100
for num in range(len(pokemon)):
if pokemon[num]['pokemon_name'] == 'Aerodactyl':
Lat = pokemon[num]['latitude']
Long = pokemon[num]['longitude']
if (Lat, Long) not in Coordinates:
Coordinates[Sighting] = (Lat, Long)
file = open("aerodactyl.txt", "a")
file.write(str(Lat) + "," + str(Long) + "\n")
file.close()
Sighting += 1
這段代碼是完全搞砸了。 'while True'循環永遠不會退出,並且在'file.close'調用之後忘記添加一對括號。 – xmcp
'for'循環應該在while循環中嗎? – Barmar
我推薦一個'set()'來代替列表的座標。 –