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初学者指南》
目录
相关文章
|
5月前
|
前端开发 关系型数据库 MySQL
基于python+mysql的宠物领养网站系统
基于python+mysql的宠物领养网站系统
82 2
|
5月前
|
Unix Linux iOS开发
创建 Python 虚拟环境
创建 Python 虚拟环境
99 1
|
4月前
|
Linux iOS开发 MacOS
【chat-gpt问答记录】python虚拟环境venv的简介及使用
【chat-gpt问答记录】python虚拟环境venv的简介及使用
53 2
|
2月前
|
Ubuntu 开发者 Python
|
8天前
|
Linux Python
linux之部署python环境&创建虚拟环境
linux之部署python环境&创建虚拟环境
|
14天前
|
机器学习/深度学习 监控 TensorFlow
使用Python实现深度学习模型:智能宠物监控与管理
使用Python实现深度学习模型:智能宠物监控与管理
44 0
|
3月前
|
数据采集 存储 API
Python虚拟环境数据共享技术解析:最佳实践与常见误区
本文探讨了Python爬虫开发中如何在虚拟环境中管理数据,提倡使用共享目录、数据库和API进行数据共享。通过创建虚拟环境、安装依赖并提供一个使用代理IP爬取微博数据的示例,阐述了如何配置代理、解析网页及保存数据到共享路径。强调了避免硬编码路径、忽视依赖管理和数据安全性的误区。
71 11
Python虚拟环境数据共享技术解析:最佳实践与常见误区
|
3月前
|
Linux iOS开发 MacOS
python的virtualenv虚拟环境常见问题和命令
`venv`是Python的内置模块,用于创建隔离的虚拟环境。创建虚拟环境如`python3 -m venv myenv`,激活环境在Windows上是`./venv/Scripts/activate`,在Unix-like系统是`source myenv/bin/activate`。退出环境用`deactivate`。`pip list`查看已安装包,`pip install`安装包,`pip freeze > requirements.txt`保存依赖。PyCharm中红色`venv`表示项目使用了虚拟环境。
69 2
 python的virtualenv虚拟环境常见问题和命令
|
2月前
|
Linux Docker Python
创建python虚拟环境并打包python文件
创建python虚拟环境并打包python文件
|
4月前
|
IDE 开发工具 数据库
python虚拟环境下 .gitignore 要忽略什么
在Python虚拟环境中,.gitignore 文件用于告诉 Git 哪些文件和目录是不需要添加到版本控制中的。以下是一个典型的 Python 虚拟环境中 .gitignore 文件的内容:
下一篇
无影云桌面