Advertisement
MyriadCx

Python knowledge base

Aug 20th, 2015
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.48 KB | None | 0 0
  1. import pickle
  2. try:
  3.     knowledge = pickle.load(open("knowledge.p", "rb"))
  4. except FileNotFoundError:
  5.     print("Knowledge file not found, creating new")
  6.     knowledge = {}
  7.     pickle.dump(knowledge, open("knowledge.p", "wb"))
  8.     knowledge = pickle.load(open("knowledge.p", "rb"))
  9.    
  10.    
  11.  
  12. def getChoice():
  13.     while True:
  14.         print("Would you like to know somthing or are you going to tell me somthing?")
  15.         choice = input("you: ")
  16.         if choice == 'know':
  17.             print("What would you like to know?")
  18.             return choice
  19.         elif choice == 'tell':
  20.             print("Ok, what are you going to tell me about?")
  21.             return choice
  22.         else:
  23.             print("Im sorry what??")
  24.            
  25. def getYesNo():
  26.         while True:
  27.             yesNo = input("you: ").lower()
  28.             if yesNo == 'yes':
  29.                 return yesNo
  30.             elif yesNo == 'no':
  31.                 return yesNo
  32.             else:
  33.                 print("Im sorry what??")
  34.  
  35. def tellMe():
  36.     thing = input("you: ")
  37.     if thing in knowledge:
  38.         definition = knowledge[thing]
  39.         print("I already know what", thing, "is. It's", definition)
  40.         return (thing, definition)
  41.     else:
  42.         print("And what's that?")
  43.         definition = input("you: ")
  44.         return (thing, definition)
  45.    
  46. def checkTell(thing, definition):
  47.         while True:
  48.             print("So", thing, "is", definition, "right?")
  49.             yesNo = getYesNo()
  50.             if yesNo == 'no':
  51.                 print("Then what is", thing, "?")
  52.                 definition = input("you: ")
  53.             else:
  54.                 print("Ok great")
  55.                 return (thing, definition)
  56.  
  57.        
  58.          
  59. while True:
  60.     choice = getChoice()
  61.     if choice == 'tell':
  62.         (thing, definition) = tellMe()
  63.         (thing, definition) = checkTell(thing, definition)
  64.         knowledge.update({thing: definition})
  65.         pickle.dump(knowledge, open("knowledge.p", "wb"))
  66.     elif choice == 'know':
  67.         skip = False
  68.         query = input("you: ")
  69.         if query == 'everything':
  70.             print("Ok here it is!\n", knowledge, '\n')
  71.             skip = True
  72.         if skip == False:
  73.             if query in knowledge:
  74.                 result = knowledge[query]
  75.                 print("I think that", query, "is", result, "... Am I right?")
  76.                 yesNo = getYesNo()
  77.                 if yesNo == 'yes':
  78.                     print("Great!")
  79.                 if yesNo == 'no':
  80.                     thing = query
  81.                     print("So what is a", thing, "?")
  82.                     definition = input("you: ")
  83.                     (thing, definition) = checkTell(thing, definition)
  84.                     knowledge.update({thing: definition})
  85.                     pickle.dump(knowledge, open("knowledge.p", "wb"))
  86.             else:
  87.                 print("Sorry I don't know about", query, "would you like to tell me about it?")
  88.                 yesNo = getYesNo()
  89.                 if yesNo == 'yes':
  90.                     thing = query
  91.                     print("So what is a", thing, "?")
  92.                     definition = input("you: ")
  93.                     (thing, definition) = checkTell(thing, definition)
  94.                     knowledge.update({thing: definition})
  95.                     pickle.dump(knowledge, open("knowledge.p", "wb"))
  96.                 else:
  97.                     print("Maybe later then")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement