Guest User

Untitled

a guest
Oct 13th, 2013
736
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.34 KB | None | 0 0
  1. class interpret():
  2.  
  3.     def Init(self, s, long_mode = True):
  4.         global accumulator
  5.        
  6.         try:
  7.             accumulator = accumulator
  8.         except:
  9.             accumulator = 0
  10.        
  11.         do = {"H": lambda: self.hello(),
  12.               "Q": lambda: self.quine(),
  13.               "9": lambda: self.beers(long_mode),
  14.               "+": lambda: self.incre(),}
  15.              
  16.         if len(s) > 1:
  17.             raise Exception("Too many instructions.")
  18.             return
  19.              
  20.         for i in list(s):
  21.             if i not in ["H", "Q", "9", "+"]:
  22.                 raise Exception("Unrecognized Instruction")
  23.                 return
  24.  
  25.         return str(do[s]())+"\n"
  26.        
  27.     def hello(self):
  28.         return "Hello, World!"
  29.        
  30.     def quine(self):
  31.         return "Q"
  32.        
  33.     def beers(self, long_mode):
  34.         song = []
  35.        
  36.         if long_mode:
  37.             for i in range(1, 98):
  38.                 i = 100-i
  39.                 song.append(str(i)+" bottles of beer on the wall, "+str(i)+" bottles of beer!\nTake one down, pass it around, "+str(i-1)+" bottles of beer on the wall!\n")
  40.             song.append("2 bottles of beer on the wall, 2 bottles of beer!\nTake one down, pass it around, 1 bottle of beer on the wall!\n")
  41.             song.append("1 bottle of beer on the wall, 1 bottle of beer!\nTake it down, pass it around, no more bottles of beer on the wall!\n")
  42.             song.append("No more bottles of beer on the wall, no more bottles of beer!\nGo to the store and buy some more, 99 bottles of beer on the wall!")
  43.            
  44.         else:
  45.             song.append("99 bottle of beer on the wall, 99 bottles of beer!\nTake one down, pass it around, 98 bottles of beer on the wall!\n")
  46.             song.append("[...]\n")
  47.             song.append("1 bottle of beer on the wall, 1 bottle of beer!\nTake it down, pass it around, no more bottles of beer on the wall!\n")
  48.             song.append("No more bottles of beer on the wall, no more bottles of beer!\nGo to the store and buy some more, 99 bottles of beer on the wall!")
  49.            
  50.         return '\n'.join(song)
  51.        
  52.     def incre(self):
  53.         global accumulator
  54.         accumulator += 1
  55.        
  56.         return accumulator
  57.        
  58.  
  59. if __name__ == "__main__":
  60.     while True:
  61.         print(interpret().Init(input("> "), False))
Advertisement
Add Comment
Please, Sign In to add comment