【问题标题】:Why is (TypeError: 'str' object cannot be interpreted as an integer) appearing on my reminder system为什么 (TypeError: 'str' object cannot be mapped as an integer) 出现在我的提醒系统上
【发布时间】:2017-06-06 19:36:05
【问题描述】:

在我的迷你助手上做了很多工作后,我在提醒系统中遇到了一个我无法修复的大错误。在哪里,要求提醒后,它崩溃了。它的意思是将它存储在一个变量和一个记事本文件中,以便在时间(按分钟设置)用完后使用。对于任何混乱的代码并显示整个代码,我很抱歉,我无法确定代码出错的地方。该代码适用于 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)))

口译员

    I am Squeep Bot, my current purpose is to have meaningless conversations with you or search the internet, like siri, cortana, or bonzi buddy

>>> remind 1 hi
Im reminding you that:  test  at  2017-05-26 13:58:31
Q: remind 1 hi
Traceback (most recent call last):
  File "U:\My Documents\py scripter\Squeepbot\Squeepbot.py", line 127, in <module>
    print("{}".format(analyse(s)))
  File "U:\My Documents\py scripter\Squeepbot\Squeepbot.py", line 108, in analyse
    writeFile(round(time.time()+(int(mins)*60),message))
TypeError: 'str' object cannot be interpreted as an integer
>>> 

任何答案都会有所帮助,即使是代码的布局

【问题讨论】:

  • 您尝试使用message 作为round() 的第二个参数,而message 是一个字符串。您将其传递给错误的函数 - 将其移到 round() 调用之外。
  • 如果使用正确的编码风格 ([Python]: PEP 8 -- Style Guide for Python Code),这些问题更容易发现(例如,writeFile(round(time.time() + (int(mins) * 60), message)),使大括号更容易计数。

标签: python python-3.4


【解决方案1】:
writeFile(round(time.time()+(int(mins)*60)),message)

【讨论】:

  • 谢谢!这真的很有帮助!
猜你喜欢
  • 1970-01-01
  • 2017-06-02
  • 2016-11-17
  • 1970-01-01
  • 2022-12-02
  • 1970-01-01
  • 2023-02-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多