Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
chatBot/info.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
61 lines (55 sloc)
2.91 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random #this package allows to choose randomly an index of a list | |
from confirmDB import connection | |
from Setup import load_setting | |
""" prints a random answer to question about favourite things """ | |
def informations(sentence): | |
answer = ["I don't want to share this with you", "If i tell you I would have to kill you", "I just don't know yet", | |
"Good question!"] | |
final = random.choice(answer) | |
print(final) | |
ownInfo(sentence) | |
""" gets information about favourites of the user""" | |
def ownInfo(sentence): | |
sep = 'favourite ' | |
info = sentence.split(sep, 1)[0] #gets the words before the word "favourite" in a sentence to isolate "favourite" and the key word | |
info = info + sep #adds the word "favourite" to the already filtered sentence | |
info = sentence.replace(info, "") #gets the key word by removing all the words previous to "favourite" and "favourite" included | |
info = info.replace(" ", "") | |
nick = load_setting(0) #get the nick of current user | |
nick = nick.lower() | |
nick = nick[:-1] | |
"""--------------------------------------------""" | |
try: | |
connect = connection() # gets the details to the database | |
cursor = connect.cursor() # establish a cursor to fetch the results wanted from the databse | |
sql = 'SELECT ' + info + ' FROM favourite WHERE NICKNAME = "' + nick + '"' #sql gets the order to select all info from the table favourite in the current user row | |
cursor.execute(sql) # order to select executed | |
myresult = cursor.fetchall() # gets all the rows of nicknames where the nickname == nick | |
l = str(myresult[0]) # l = ('favourite food',) / (None,) | |
if "None" in l: | |
final = (l[1:]) | |
final = final[:-2] | |
else: | |
final = (l[2:]) | |
final = final[:-3] | |
if final == "None": | |
getInfo(nick, info) | |
else: | |
print("Your favourite ", info.lower() , " is ", final) | |
except: | |
getInfo(nick, info) | |
""" "add" new values to table favourite """ | |
def getInfo(nick, info): | |
try: | |
print("I don't know yours, what is it? ") | |
ans = input("") | |
connect = connection() # gets the details to the database | |
cursor = connect.cursor() # establish a cursor to fetch the results wanted from the databse | |
if info in "footballplayer": | |
info = "football_player" | |
sql = 'UPDATE favourite SET '+info+'="'+ans+'" WHERE NICKNAME="'+ nick + '"' #sql gets the order to update all info from the table favourite in the current user row | |
cursor.execute(sql) # order to select executed | |
connect.commit() # tell the database to save all the changes | |
print("Ok, I will try not to forget!") | |
except: | |
print("I am not able to rember that, I am sorry.") |