点击(此处)折叠或打开
- [18:15 t ~/PycharmProjects/talen]$ ll
- -rw-rw-r--. 1 t t 2064 4月 22 18:15 critter_caretaker.py
- -rw-rw-r--. 1 t t 17734 4月 22 16:55 critter.graphml
-
点击(此处)折叠或打开
- #!/usr/bin/env python
- #-*- coding:utf-8 -*-
- '''
- '''
-
- class Critter(object):
- '''
- #一只需要细心呵护的虚拟宠物
- #critter caretaker
- #构造器,三个公共属性
- '''
- def __init__(self, name, hunger=0, boredom=0):
- self.name=name
- self.hunger=hunger
- self.boredom=boredom
- #单独处理时间流逝
- def __pass_time(self):
- self.hunger += 1
- self.boredom += 1
- #处理宠物性情
- @property
- def mood(self):
- unhappiness= self.boredom + self.hunger
- print("unhappniess num : %s" %unhappiness)
- if unhappiness 5:
- m = "happy"
- elif 5 = unhappiness = 10:
- m = "okay"
- elif 10 = unhappiness = 15:
- m = "bad"
- else:
- m = "mad"
- return m
- #处理
- def talk(self):
- print("I'm %s and I feel %s now!\n"%(self.name,self.mood))
- self.__pass_time()
- def eat(self,food=4):
- print("Think you")
- self.hunger -= food
- if self.hunger 0 :
- self.hunger = 0
- #self.__pass_time()
- def play(self,fun=4):
- print("Wheee")
- self.boredom -= fun
- if self.boredom 0 :
- self.boredom = 0
- #self.__pass_time()
-
- def main():
- #创建实例对象
- crit_name=raw_input( "Your pet name:" )
- print("Pet name :%s"%crit_name)
- crit = Critter(crit_name)
- choice=None
- while choice != 0:
- print\
- ('''
- Critter caretaker
- 0 - Quit
- 1 - Listen to your critter
- 2 - Feed your critter
- 3 - Play with your critter
-
-
- ''')
- choice=raw_input("请输入你的选择:")
- if choice == "0":
- print("Goodbye!")
- break
- elif choice == "1":
- #听宠物说话
- crit.talk()
- elif choice == "2":
- #给宠物吃饭
- crit.eat()
- elif choice == "3":
- #陪宠物玩
- crit.play()
- else:
- print("Sorry %s"%choice)
- main()
点击(此处)折叠或打开
- [18:20 t ~/PycharmProjects/talen]$ python critter_caretaker.py
- Your pet name:talen
- Pet name :talen
-
- Critter caretaker
- 0 - Quit
- 1 - Listen to your critter
- 2 - Feed your critter
- 3 - Play with your critter
-
-
-
- 请输入你的选择:1
- unhappniess num : 0
- I'm talen and I feel happy now!
-
-
- Critter caretaker
- 0 - Quit
- 1 - Listen to your critter
- 2 - Feed your critter
- 3 - Play with your critter
-
-
-
- 请输入你的选择:2
- Think you
-
- Critter caretaker
- 0 - Quit
- 1 - Listen to your critter
- 2 - Feed your critter
- 3 - Play with your critter
-
-
-
- 请输入你的选择:1
- unhappniess num : 3
- I'm talen and I feel happy
-
-
- Critter caretaker
- 0 - Quit
- 1 - Listen to your critter
- 2 - Feed your critter
- 3 - Play with your critter
-
-
-
- 请输入你的选择:3
- Wheee
-
- Critter caretaker
- 0 - Quit
- 1 - Listen to your critter
- 2 - Feed your critter
- 3 - Play with your critter
-
-
-
- 请输入你的选择:2
- Think you
-
- Critter caretaker
- 0 - Quit
- 1 - Listen to your critter
- 2 - Feed your critter
- 3 - Play with your critter
-
-
-
- 请输入你的选择:3
- Wheee
-
- Critter caretaker
- 0 - Quit
- 1 - Listen to your critter
- 2 - Feed your critter
- 3 - Play with your critter
-
-
-
- 请输入你的选择:2
- Think you
-
- Critter caretaker
- 0 - Quit
- 1 - Listen to your critter
- 2 - Feed your critter
- 3 - Play with your critter
-
-
-
- 请输入你的选择:1
- unhappniess num : 3
- I'm talen and I feel happy now!
-
-
- Critter caretaker
- 0 - Quit
- 1 - Listen to your critter
- 2 - Feed your critter
- 3 - Play with your critter
-
-
-
- 请输入你的选择:1
- unhappniess num : 5
- I'm talen and I feel okay
-
-
- Critter caretaker
- 0 - Quit
- 1 - Listen to your critter
- 2 - Feed your critter
- 3 - Play with your critter
-
-
-
- 请输入你的选择:0
-
- [18:21 t ~/PycharmProjects/talen]$
参考:《python初学者指南》