Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 27th, 2012  |  syntax: None  |  size: 5.84 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Connecting and changing .txt-values to/from a mainfunction
  2. Rats
  3. Berta/2/4/3/0
  4. Oscar/5/0/3/2
  5. John/-1/-6/-5/-9
  6.        
  7. #The class
  8. class Rat(object):
  9.     """Defines my class"""
  10.     def __init__(self, rat_name, sleep, joy, full, happiness):
  11.         self.__rat_name = rat_name
  12.         self.__sleep = sleep
  13.         self.__joy = joy
  14.         self.__full = full
  15.         self.__happiness = happiness
  16.  
  17.  
  18.     def setfull(self, value, absolute = False):
  19.         """Gives the full attribute a value"""
  20.         try:
  21.             value = int(value)
  22.             if absolute:
  23.                 self.__full = value
  24.             else:
  25.                 self.__full += value
  26.             return True
  27.         except ValueError:
  28.             return False
  29.  
  30.     def getfull(self):
  31.         """Gives a fullvalue"""
  32.         return self.__full
  33.  
  34.     def setfull(self, x):
  35.         x = int(x)
  36.         """acess to the full attribute"""
  37.         self.__full = x
  38.  
  39.     def sethappiness(self, value, absolute = False):
  40.         """Gives the happiness attribute a value"""
  41.         try:
  42.             value = int(value)
  43.             if absolute:
  44.                 self.__happiness = value
  45.             else:
  46.                 self.__happiness += value
  47.             return True
  48.         except ValueError:
  49.             return False
  50.  
  51.     def gethappiness(self):
  52.         """Gives happiness value"""
  53.         return self.__happiness
  54.  
  55.     def sethappiness(self, x):
  56.         """access to the happiness attribute"""
  57.         x = int(x)
  58.         self.__happiness = x
  59.  
  60.     def setsleep(self, value, absolute = False):
  61.         """Gives the sleep attribute a value"""
  62.         try:
  63.             value = int(value)
  64.             if absolute:
  65.                 self.__sleep = value
  66.             else:
  67.                 self.__sleep += value
  68.             return True
  69.         except ValueError:
  70.             return False
  71.  
  72.     def getsleep(self):
  73.         """Gives a sleep value"""
  74.         return self.__sleep
  75.  
  76.     def setsleep(self, x):
  77.         """access to the sleep attribute"""
  78.         x = int(x)
  79.         self.__sleep = x
  80.  
  81.     def setjoy(self, value, absolute = False):
  82.         """Gives a value to the joy attribute"""
  83.         try:
  84.             value = int(value)
  85.             if absolute:
  86.                 self.__joy = value
  87.             else:
  88.                 self.__joy += value
  89.             return True
  90.         except ValueError:
  91.             return False
  92.  
  93.     def getjoy(self):
  94.         """Gives a joy value"""
  95.         return self.__joy
  96.  
  97.     def setjoy(self, x):
  98.         """access to the joy attribute"""
  99.         x = int(x)
  100.         self.__joy = x
  101.  
  102.  
  103. # main menu functions
  104.     def cheese(self):
  105.         """Feeds the pet"""
  106.         print("- Mmmm cheese!")
  107.         self.__full += 3
  108.         self.__sleep += 1
  109.         self.__joy += 1
  110.         return self.__full, self.__sleep, self.__joy
  111.  
  112.     def play(self):
  113.         """Plays with the rat"""
  114.         print("- Oh, I'm having fun!")
  115.         self.__full -= 2
  116.         self.__sleep += 2
  117.         self.__joy += 4
  118.         self.__happiness += 2
  119.         return self.__full, self.__sleep, self.__joy, self.__happiness
  120.  
  121.  
  122.     def tosleep(self):
  123.         """Let the rat sleep"""
  124.         print("Zzzzzzz")
  125.         self.__sleep -= 7
  126.         self.__joy += 1
  127.         return self.__sleep, self.__joy
  128.  
  129.     def trap(self):
  130.         """A mousetrap"""
  131.         if self.__full > 5:
  132.             print("The rat is to full to be fooled by a simple mousetrap")
  133.         else:
  134.             print("The rat escaped with a broken tail!")
  135.             self.__joy -= 2
  136.             self.__happiness -=2
  137.  
  138.         return self.__full, self.__sleep, self.__joy, self.__happiness
  139.  
  140.  
  141.  
  142.  
  143.     def __str__(self):
  144.         """Returns a string that describes the mood of the rat"""
  145.         mood =self.rat_name + " är: "
  146.         if self.__joy > 5:
  147.             mood += "Glad, "
  148.         else:
  149.             mood += "Pissed, "
  150.         if self.__full > 8:
  151.             mood += "overfed, "
  152.         elif self.__full > 0:
  153.             mood += "full, "
  154.         elif self.__full < -5:
  155.             mood += "starving, "
  156.         else:
  157.             mood += "craving cheese and "
  158.         if self.__sleep > 7:
  159.             mood += "very tired and "
  160.         elif self.__sleep > 0:
  161.             mood += "sleepy and "
  162.         else:
  163.             mood += "well rested and "
  164.         if self.__happiness > 7:
  165.             mood += "SUPER HAPPY!"
  166.         elif self.__happiness > 0:
  167.             mood += "quite happy!"
  168.         else:
  169.             mood += "unhappy..."
  170.         return mood
  171.  
  172.  
  173. # The list
  174. def listan():
  175.     """Opens and sorts the list"""
  176.     infil = open("ratlist.txt", "r")
  177.     infil.readline
  178.     rats = []
  179.  
  180.     for row in infil:
  181.  
  182.         lista = row.split("/")
  183.         print(lista)
  184.         ratname = lista[0]
  185.         ratsleep = int(lista[1])
  186.         ratjoy = int(lista[2])
  187.         ratfull = int(lista[3])
  188.         rathappiness = int(lista[4].strip())
  189.         t = Rat(ratname, ratsleep, ratjoy, ratfull, rathappiness)
  190.         rats.append(t)
  191.     print("Finished")    
  192.     return rats
  193.  
  194.  
  195.  
  196. # the main menu    
  197. def main():
  198.     """The menu"""
  199.     ratzinger = listan()
  200.     choice = None
  201.     while choice != "1":
  202.         print
  203.         ("""
  204.         The ratkeeper
  205.  
  206.         1 - Buy a cat
  207.         2 - Listen to your rat
  208.         3 - Let the rat sleep
  209.         4 - Give the rat cheese
  210.         5 - Prepare a mousetrap
  211.         6 - Play with the rat
  212.         7 - Change the values of your rat
  213.         8 - Know the name of my rat
  214.         """)
  215.  
  216.         choice = input("I want to: ")
  217.  
  218.         if val == "1":
  219.             print(rat_name, "was tortured and eaten, bye.")
  220.         elif val == "2":
  221.             print(ratzinger)
  222.  
  223.         elif val == "3":
  224.             ratzinger.tosleep()
  225.  
  226.         elif val == "4":
  227.             ratzinger.cheese()
  228.  
  229.         elif val == "5":
  230.             ratzinger.trap()
  231.  
  232.         elif val == "6":
  233.             ratzinger.play()
  234.  
  235.         elif val == "7":
  236.             print()
  237.  
  238.         elif val == "8":
  239.             print()
  240.  
  241.         else:
  242.             print("Choose a number between one and eight!")
  243.  
  244. main()
  245.  
  246. input()