daily pastebin goal
58%
SHARE
TWEET

Untitled

a guest Jan 20th, 2019 49 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import random
  2.  
  3.  
  4. class MadLibs:
  5.  
  6.     def __init__(self):
  7.         self.woordenschat = {}
  8.  
  9.     def leren(self, categorie, nieuwe_woorden):
  10.         if isinstance(nieuwe_woorden, str):
  11.             nieuwe_woorden = {nieuwe_woorden.lower()}
  12.         else:
  13.             nieuwe_woorden = set(w.lower() for w in nieuwe_woorden)
  14.         if categorie.lower() in self.woordenschat:
  15.             self.woordenschat[categorie].update(nieuwe_woorden)
  16.         else:
  17.             self.woordenschat.update({categorie.lower(): nieuwe_woorden})
  18.  
  19.     def suggereren(self, categorie):
  20.         assert categorie.lower() in self.woordenschat, 'onbekende categorie'
  21.         woord = random.choice(list(self.woordenschat[categorie.lower()]))
  22.         if categorie.isupper():
  23.             woord = woord.upper()
  24.         elif categorie[0].isupper():
  25.             woord = woord.capitalize()
  26.         return woord
  27.  
  28.     def invullen(self, zin):
  29.         zin = zin.split('_')
  30.         for index, woord in enumerate(zin):
  31.             if index % 2:
  32.                 assert woord.lower() in self.woordenschat, 'onbekende categorie'
  33.                 invulwoord = self.suggereren(woord)
  34.                 zin[index] = invulwoord
  35.         return ''.join(zin)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top