python - % : 'NoneType' and 'int' 不支持的操作数类型

标签 python int nonetype

我正在开发一个项目,我在网上找到了一个简单的 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/

相关文章:

python - 如何使用 gluUnproject?

python - Python IDLE 在 Windows 上使用什么终端

c - C 编程语言的 TC++ 中 int i=00100 错误

python - 描述符 'getter' 需要一个 'property' 对象,但收到一个 'function'

python - 如何在不同的OpenCV版本中使用 `cv2.findContours`?

c++ - 无限循环的 while 循环内的 switch 语句

C# 整数作为回调中的引用传递?

python - 避免检查记录器是否存在

python - 从YahooFinance获取价格数据会导致: AttributeError 'nonetype' object has no attribute 'text'

python - 为什么返回 Nonetype?