Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1817684
  • 博文数量: 276
  • 博客积分: 1574
  • 博客等级: 上尉
  • 技术积分: 2894
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-26 23:23
个人简介

生活的美妙在于,不知道一下秒是惊艳还是伤神,时光流转,珍惜现在的拥有的时光

文章分类

全部博文(276)

文章存档

2017年(17)

2016年(131)

2015年(63)

2013年(2)

2012年(32)

2011年(31)

分类: Python/Ruby

2016-04-22 18:22:15


点击(此处)折叠或打开

  1. [18:15 t ~/PycharmProjects/talen]$ ll
  2. -rw-rw-r--. 1 t t 2064 4月 22 18:15 critter_caretaker.py
  3. -rw-rw-r--. 1 t t 17734 4月 22 16:55 critter.graphml


点击(此处)折叠或打开

  1. #!/usr/bin/env python
  2. #-*- coding:utf-8 -*-
  3. '''
  4. '''

  5. class Critter(object):
  6.     '''
  7.     #一只需要细心呵护的虚拟宠物
  8.     #critter caretaker
  9.     #构造器,三个公共属性
  10.     '''
  11.     def __init__(self, name, hunger=0, boredom=0):
  12.         self.name=name
  13.         self.hunger=hunger
  14.         self.boredom=boredom
  15.     #单独处理时间流逝
  16.     def __pass_time(self):
  17.         self.hunger += 1
  18.         self.boredom += 1
  19.     #处理宠物性情
  20.     @property
  21.     def mood(self):
  22.         unhappiness= self.boredom + self.hunger
  23.         print("unhappniess num : %s" %unhappiness)
  24.         if unhappiness < 5:
  25.             m = "happy"
  26.         elif 5 <= unhappiness <= 10:
  27.             m = "okay"
  28.         elif 10 <= unhappiness <= 15:
  29.             m = "bad"
  30.         else:
  31.             m = "mad"
  32.         return m
  33.     #处理
  34.     def talk(self):
  35.         print("I'm %s and I feel %s now!\n"%(self.name,self.mood))
  36.         self.__pass_time()
  37.     def eat(self,food=4):
  38.         print("Think you")
  39.         self.hunger -= food
  40.         if self.hunger < 0 :
  41.             self.hunger = 0
  42.         #self.__pass_time()
  43.     def play(self,fun=4):
  44.         print("Wheee")
  45.         self.boredom -= fun
  46.         if self.boredom < 0 :
  47.             self.boredom = 0
  48.         #self.__pass_time()

  49. def main():
  50.     #创建实例对象
  51.     crit_name=raw_input( "Your pet name:" )
  52.     print("Pet name :%s"%crit_name)
  53.     crit = Critter(crit_name)
  54.     choice=None
  55.     while choice != 0:
  56.         print\
  57.         ('''
  58.         Critter caretaker
  59.         0 - Quit
  60.         1 - Listen to your critter
  61.         2 - Feed your critter
  62.         3 - Play with your critter


  63.         ''')
  64.         choice=raw_input("请输入你的选择:")
  65.         if choice == "0":
  66.             print("Goodbye!")
  67.             break
  68.         elif choice == "1":
  69.             #听宠物说话
  70.             crit.talk()
  71.         elif choice == "2":
  72.             #给宠物吃饭
  73.             crit.eat()
  74.         elif choice == "3":
  75.             #陪宠物玩
  76.             crit.play()
  77.         else:
  78.             print("Sorry %s"%choice)
  79. main()


点击(此处)折叠或打开

  1. [18:20 t ~/PycharmProjects/talen]$ python critter_caretaker.py
  2. Your pet name:talen
  3. Pet name :talen

  4.         Critter caretaker
  5.         0 - Quit
  6.         1 - Listen to your critter
  7.         2 - Feed your critter
  8.         3 - Play with your critter


  9.         
  10. 请输入你的选择:1
  11. unhappniess num : 0
  12. I'm talen and I feel happy now!


  13.         Critter caretaker
  14.         0 - Quit
  15.         1 - Listen to your critter
  16.         2 - Feed your critter
  17.         3 - Play with your critter


  18.         
  19. 请输入你的选择:2
  20. Think you

  21.         Critter caretaker
  22.         0 - Quit
  23.         1 - Listen to your critter
  24.         2 - Feed your critter
  25.         3 - Play with your critter


  26.         
  27. 请输入你的选择:1
  28. unhappniess num : 3
  29. I'm talen and I feel happy


  30.         Critter caretaker
  31.         0 - Quit
  32.         1 - Listen to your critter
  33.         2 - Feed your critter
  34.         3 - Play with your critter


  35.         
  36. 请输入你的选择:3
  37. Wheee

  38.         Critter caretaker
  39.         0 - Quit
  40.         1 - Listen to your critter
  41.         2 - Feed your critter
  42.         3 - Play with your critter


  43.         
  44. 请输入你的选择:2
  45. Think you

  46.         Critter caretaker
  47.         0 - Quit
  48.         1 - Listen to your critter
  49.         2 - Feed your critter
  50.         3 - Play with your critter


  51.         
  52. 请输入你的选择:3
  53. Wheee

  54.         Critter caretaker
  55.         0 - Quit
  56.         1 - Listen to your critter
  57.         2 - Feed your critter
  58.         3 - Play with your critter


  59.         
  60. 请输入你的选择:2
  61. Think you

  62.         Critter caretaker
  63.         0 - Quit
  64.         1 - Listen to your critter
  65.         2 - Feed your critter
  66.         3 - Play with your critter


  67.         
  68. 请输入你的选择:1
  69. unhappniess num : 3
  70. I'm talen and I feel happy now!


  71.         Critter caretaker
  72.         0 - Quit
  73.         1 - Listen to your critter
  74.         2 - Feed your critter
  75.         3 - Play with your critter


  76.         
  77. 请输入你的选择:1
  78. unhappniess num : 5
  79. I'm talen and I feel okay


  80.         Critter caretaker
  81.         0 - Quit
  82.         1 - Listen to your critter
  83.         2 - Feed your critter
  84.         3 - Play with your critter


  85.         
  86. 请输入你的选择:0

  87. [18:21 t ~/PycharmProjects/talen]$

参考:《python初学者指南》
阅读(2331) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~