【发布时间】:2016-08-31 18:08:29
【问题描述】:
我正在开发一个程序,我即将结束它,我刚刚在我的聊天机器人中实现了让机器人记住已经与他们交谈过的用户的能力。通过将用户给我的聊天机器人的名称保存到文本文件很容易完成,但是为了结束我的程序,我希望能够跟踪用户与我的聊天机器人交谈的次数,但我不确定如何去做吧。
我知道它需要存储在一个文本文件中,但我如何为每个用户提供他们的访问次数?
#Defining the YouTube Channel function
def Maximus():
#Holding the end user's name to make the chatbot more friendly
userName = raw_input ("\nPlease enter your name: ")
if userName in open('usernames.txt').read(): #Checks to see if user is pre-existing
print ("Welcome back, %s. Good to see you again!" % (userName)) #If user is pr-existing, send this message
else:
print ("Nice to meet you %s, I'm Maximus, the friendly bot that helps to answer any questions you may have about YouTube's website!\nType quit to go back to the main menu." % (userName))
fw = open('usernames.txt', 'a')
fw.write("%s\n" % (userName)) #Creates the new user, which Maximus remembers
fw.close()
显示“欢迎回来”消息的位置,在其末尾,我想显示用户登录与聊天机器人交谈的次数
【问题讨论】:
-
到目前为止你尝试过什么?您可以使用
collections.Counter。 -
@Bahrom 这是我第一次写入文件,因为我刚刚开始学习 Python,所以除了将用户名保存到文件中,没有别的
-
到目前为止发布您的代码(只是基本部分)。有一个样本输入/输出会有所帮助。这样的问题相当广泛。例如,你是如何写名字的?