我正在开发一个项目,我在网上找到一个简单的IP地址来谷歌地图对话,找到一个简单的汽车跟踪过程的位置。总共有4个文件,但我被困在main.py脚本上。我一直试图让它工作好几天。我取得了进展,但现在我收到了错误:
TypeError:%不支持的操作数类型:'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)
任何帮助都会非常感激!
问候
答案 0 :(得分:1)
在第
行sys.stdout.write ("\restarting in %d seconds ") % (SleepTime-i)
Python认为你正在使用mod运算符对sys.stdout.write(即None
)和SleepTime - i
(这是一个int)的结果进行数学运算。这是因为你有一个早期的括号。你想要的是打印整个结果:
sys.stdout.write ("restarting in %d seconds " % (SleepTime-i))
作为旁注,格式化的%是Python中的frowned upon,而不是string.format。
答案 1 :(得分:1)
在这一行:
sys.stdout.write ("\restarting in %d seconds ") % (SleepTime-i)
您使用的分组不正确。这是它正在做的事情,用更多的括号说明:
(sys.stdout.write ("\restarting in %d seconds ")) % (SleepTime-i)
sys.stdout.write()
返回None
,您正在None % integer
。您需要将SleepTime-i
放在调用中,因此它适用于字符串而不是函数调用:
sys.stdout.write("\restarting in %d seconds " % (SleepTime-i))