我正在开发一个项目,我在网上找到了一个简单的 IP 地址到谷歌地图的对话,以查找简单的汽车跟踪过程的位置。总共有 4 个文件,但我卡在了 main.py 脚本上。几天来我一直在努力让它发挥作用。我取得了进展,但现在收到错误:
类型错误:% 不支持的操作数类型:“NoneType”和“int”
这是脚本:
#!/usr/bin/python
import sys,time,geolocation,publisher
from subprocess import call
SleepTime = 10 # seconds
_lat = 0.00
_lon = 0.00
def maintain():
global _lat
global _lon
(lat,lon,accuracy) = geolocation.getLocation()
if(lat != _lat or lon !=_lon):
data = str(lat) + "," + str(lon) + "," + str(accuracy)
print ("publishing") , data
publisher.publishtoInternet(data)
_lat = lat
_lon = lon
else:
print ("no change in coordinates")
print ("program begins")
while True:
try:
maintain()
except Exception as inst:
print (type)(inst), ('exception captured')
print (inst)
sys.stdout.flush()
#file = open('/tmp/loctracker.error.log','a')
#file.write('exception occured, trying to reboot')
#file.close()
#call(["sudo","reboot"])
#break
for i in range(0,SleepTime):
sys.stdout.write ("\restarting in %d seconds ") % (SleepTime-i)
sys.stdout.flush()
time.sleep(1)
任何帮助将不胜感激!
问候
最佳答案
在行
sys.stdout.write ("\restarting in %d seconds ") % (SleepTime-i)
Python 认为您正在使用 mod 运算符对 sys.stdout.write(None
)和 SleepTime - i
(这是一个整数)。这是因为你有一个早期的括号。您想要的是打印整个结果:
sys.stdout.write ("restarting in %d seconds " % (SleepTime-i))
作为旁注,格式化的%是frowned upon在 Python 中支持 string.format。
关于python - % : 'NoneType' and 'int' 不支持的操作数类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37311408/