python虚拟宠物

简介: 点击(此处)折叠或打开 [18:15 t ~/PycharmProjects/talen]$ ll -rw-rw-r--.

点击(此处)折叠或打开

  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初学者指南》
目录
相关文章
|
2月前
|
前端开发 关系型数据库 MySQL
基于python+mysql的宠物领养网站系统
基于python+mysql的宠物领养网站系统
36 2
|
4月前
|
小程序 Python
用 Python 制作一个桌面宠物,好玩!
用 Python 制作一个桌面宠物,好玩!
|
4月前
|
小程序 Python
用Python制作一个桌面宠物,真好玩!
用Python制作一个桌面宠物,真好玩!
|
3月前
|
Linux Docker Python
Python实战技术 - Python虚拟隔离环境 和 Docker技术
Python实战技术 - Python虚拟隔离环境 和 Docker技术
197 0
|
9月前
|
SQL 前端开发 JavaScript
基于python+django的宠物商店-宠物管理系统
该系统是基于python+django开发的宠物商店-宠物管理系统。是给师妹开发的课程作业。现将源码开放给大家。大家学习过程中,如遇问题可以在github咨询作者。
148 0
|
8月前
|
Python
搭建基于python或Django项目的虚拟隔离环境
搭建基于python或Django项目的虚拟隔离环境
85 0
|
10月前
|
机器学习/深度学习 数据挖掘 Linux
python虚拟开发环境
python虚拟开发环境
|
11月前
|
传感器 监控 物联网
饭碗空空 猫猫流泪 用HaaS Python DIY一个宠物自动投食器 再也不用担心主子挨饿
饭碗空空 猫猫流泪 用HaaS Python DIY一个宠物自动投食器 再也不用担心主子挨饿
154 0
|
数据采集 机器学习/深度学习 JavaScript
基于 python的猫、狗、鼠、兔宠物识别系统
基于 python的猫、狗、鼠、兔宠物识别系统
197 0
基于 python的猫、狗、鼠、兔宠物识别系统
|
存储 安全 前端开发
基于Python和Django实现的虚拟网络银行
基于Python和Django实现的虚拟网络银行
177 0
基于Python和Django实现的虚拟网络银行

热门文章

最新文章