Python 为什么(TypeError:';str';对象不能解释为整数)出现在我的提醒系统上

Python 为什么(TypeError:';str';对象不能解释为整数)出现在我的提醒系统上,python,python-3.4,Python,Python 3.4,在我的迷你助手上做了很多工作之后,我遇到了一个无法在提醒系统中修复的大错误。在请求提醒后,它崩溃了。这意味着将其存储在变量和记事本文件中,以便在时间(按分钟设置)用完后使用。很抱歉出现了混乱的代码并显示了整个代码,我无法确定代码哪里出错了。代码是针对python 3.4的 代码 #import win32com.client as wincl import webbrowser, os, datetime, time, re #speak = wincl.Dispatch("SAPI.SpVoi

在我的迷你助手上做了很多工作之后,我遇到了一个无法在提醒系统中修复的大错误。在请求提醒后,它崩溃了。这意味着将其存储在变量和记事本文件中,以便在时间(按分钟设置)用完后使用。很抱歉出现了混乱的代码并显示了整个代码,我无法确定代码哪里出错了。代码是针对python 3.4的

代码

#import win32com.client as wincl
import webbrowser, os, datetime, time, re
#speak = wincl.Dispatch("SAPI.SpVoice")
global speech
speech=1
print("I am Squeep Bot, my current purpose is to have meaningless conversations with you or search the internet, like siri, cortana, or bonzi buddy")
reminderTime=[]
reminderMessage=[]

def writeFile(timestamp,message):
    f=open("reminders.txt","a")
    f.write(str(timestamp))
    f.write(",")
    f.write(message)
    f.write(",")
    f.close()

def loadFile():
    f=open("reminders.txt","r")
    f=f.read()
    timestamp=""
    for i in range (len(f)):
        if f[i]==",":
            if timestamp.isdigit()==True:
                reminderTime.append(int(timestamp))
                timestamp=""
            else:
                reminderMessage.append(timestamp)
                timestamp=""
        else:
            timestamp=timestamp+f[i]
    print(timestamp)

loadFile()
def unixToTime(UnixTime):
    return datetime.datetime.fromtimestamp(int(UnixTime)).strftime('%Y-%m-%d %H:%M:%S')

def readReminders(reminderTime,reminderMessage):
    for i in range (len(reminderTime)):
        if reminderTime[i]<time.time():
            print("Im reminding you that: ",reminderMessage[i], " at ",unixToTime(reminderTime[i]))
            reminderTime.pop(i)
            reminderMessage.pop(i)

def analyse(user_input):
    global speech
    ui_lower = user_input.lower()
    #conversation
    if ui_lower in ["hi", "hello"]:
        r = "Hello"
elif all(x in ui_lower for x in ("who", "you")):
    r = "I am Squeep Bot, my current purpose is to have meaningless conversations with you or search the internet, like siri, cortana, or... bonzi buddy"

elif all(x in ui_lower for x in ("how","are","you")):
    r = "I'm Ok"

elif all(x in ui_lower for x in ("what","favorite","color")):
    r = "I like Green"

elif all(x in ui_lower for x in ("what","favorite","food")):
    r = "I am a computer program, I do not eat"

elif all(x in ui_lower for x in ("are","we","friend")):
    r = "Yes, we are friends"

elif all(x in ui_lower for x in ("we","are","friend")):
    r = "why thank you"

elif all(x in ui_lower for x in ("you","not","friend")):
    r = "Gasp how could you"

elif all(x in ui_lower for x in ("i","dont","hate","you")):
    r = "ok umm thanks"

elif all(x in ui_lower for x in ("cortana", "better")):
    r = "that is true"

elif all(x in ui_lower for x in ("i","hate","you")):
    r = "Why must you be so cruel"

#functions
elif all(x in ui_lower for x in ("speech","off")):
    speech=0
    r = "ok"

elif all(x in ui_lower for x in ("speech","on")):
    speech=1
    r = "ok"


elif all(x in ui_lower for x in ("search")):
    url="https://www.google.co.uk/search?safe=strict&site=&source=hp&q="+ui_lower[6:]+"&oq=test+search&gs_l=hp.3..0l10.46616.51804.0.52437.23.17.4.2.2.0.148.1449.13j3.16.0....0...1c.1.64.hp..1.19.1245.0..35i39k1j0i131k1j0i67k1j0i20k1j0i10i67k1.WktWginLj04"
    webbrowser.open_new(url)
    r = "ok"

elif all(x in ui_lower for x in ("open")):
    os.system("start "+str(ui_lower)[5:])
    r = "Opening"

#Reminders
elif all(x in ui_lower for x in ("remind")):
    mins=str(re.findall(r' +[0-9]+ ', ui_lower))[3:-3]
    message=ui_lower[8+len(mins):]
    reminderMessage.append(message)
    reminderTime.append(round(time.time()+(int(mins)*60)))
    #print(reminder)
    mins = str(mins)
    writeFile(round(time.time()+(int(mins)*60),message))
    r = "reminder saved"
else:
    r = "I'm unable to comply"

if speech==1:
    #speak.Speak(r)
    return r
running=1
while running==1:
    user_input=[""]
    user_input[0] = input(">>> ")
    for s in (user_input):
        if user_input[0].lower()=="end":
            print("A: Goodbye")
            running=0
            break
        readReminders(reminderTime,reminderMessage)
        print("Q: {}".format(s))
        print("{}".format(analyse(s)))
#将win32com.client作为wincl导入
导入webbrowser、os、datetime、time、re
#speak=wincl.Dispatch(“SAPI.SpVoice”)
全球演讲
语音=1
打印(“我是Squeep机器人,我目前的目的是与你进行无意义的对话或搜索互联网,如siri、cortana或bonzi buddy”)
提醒时间=[]
提醒消息=[]
def writeFile(时间戳、消息):
f=打开(“emememptions.txt”,“a”)
f、 写入(str(时间戳))
f、 写(“,”)
f、 写(信息)
f、 写(“,”)
f、 关闭()
def loadFile():
f=打开(“rementers.txt”、“r”)
f=f.read()
timestamp=“”
对于范围内的i(len(f)):
如果f[i]==“,”:
如果timestamp.isdigit()=真:
提醒时间.append(int(时间戳))
timestamp=“”
其他:
RememberMessage.append(时间戳)
timestamp=“”
其他:
时间戳=时间戳+f[i]
打印(时间戳)
加载文件()
def unixToTime(UnixTime):
return datetime.datetime.fromtimestamp(int(UnixTime)).strftime(“%Y-%m-%d%H:%m:%S”)
def读卡器提醒(提醒时间、提醒信息):
对于范围内的i(len(提醒时间)):
如果提醒时间[i]>>提醒1嗨
我提醒你:2017-05-26 13:58:31考试
问:你好
回溯(最近一次呼叫最后一次):
文件“U:\My Documents\py scripter\Squeepbot\Squeepbot.py”,第127行,在
打印(“{}”。格式(分析)
文件“U:\My Documents\py scripter\Squeepbot\Squeepbot.py”,第108行,在分析中
writeFile(四舍五入(time.time()+(int(min)*60),消息))
TypeError:“str”对象不能解释为整数
>>> 

任何答案都会很有帮助,即使您试图使用
message
作为
round()
的第二个参数,并且
message
是一个字符串。您将其传递给了错误的函数-将其移到
round()
调用之外。如果使用了正确的编码样式()(例如
writeFile(round(time.time()+(int(mins)*60),message))
,这些问题更容易发现,使大括号更易于计数。谢谢!这真的很有帮助!
writeFile(round(time.time()+(int(mins)*60)),message)
writeFile(round(time.time()+(int(mins)*60)),message)