te42

news/2025/2/13 6:36:37/
# -*-coding:utf-8-*-  
class TheThing(object):def __init__(self):self.number = 0#初始化  .number是self的一个属性   self就是自己def some_function(self):print"i got called."#类下面的函数 打印xxxxxdef add_me_up(self,more):self.number += more#对self.number加morereturn self.number#two different things
a = TheThing()#初始化
b= TheThing()a.some_function()
b.some_function()print a.add_me_up(20)
print b.add_me_up(30)print a.number
print b.number#study this. this is how you pass a variable
#feom one class to another. you will need this
class TheMultiplier(object):def __init__(self,base):self.base = base  #base是再后面的参数 self是前面的参数def do_it(self,m):return m * self.basex = TheMultiplier(a.number)   #等价与x 现在变成一个object 并且初始化 让x.base=a.number
print x.base
print x.do_it(b.number) #等价于 do_it(x,b.number)返回值是 x.base*b.number


from sys import exit
from random import randint  #aimed to create random numberclass Game(object):def __init__(self,start):self.die  = ["you died.you kinda suck at this.","nice job, you died ... jackass.","suck a luser.","i have a puppy that's better at this."]self.start = startdef play(self):next = self.startwhile True:#print "\n-------"room = getattr(self,next)next= room()def death(self):print self.die[randint(0,len(self.die)-1)]exit(1)def central_corridor(self):print"the Gothons of planet percal #25 have invaded your ship an destroyed"print"your entire crew. you are the last surviving menber an your last"print"mission is to get the neutron destruct bomb from the weapons armory"print"put it in the brige, and blow the ship up after geting into an"print"escape pod"print"\n"print"you're running down the central corridor to the weapons armory when"print"a Gothon jumps out, red scaly skin, ark grimy teeth, and evil clown costume"print"flowing around his hate filled bidy.he's blocking the door to the"print"armory and about to pull a weapon to blast you. "action = raw_input(">")if action == "shoot":print"quick on the draw you yank out your blaster and fire it at the Gothon."print"his clown costume is flowing and moving around his body, which throws"print"off your aim. your laser hits his costume but misses him entirely. this"print"completely ruins his brand new costume his mother bought him, which"print"makes him fly into an insane rage and blast you repeatedly in the face until"print"you are dead. then he eats you."return "death"elif action == "dodge":print"like a world class boxer you dodge, weave, slip and slide right"print"as the Gothon's blaster cranks a laser past your head."print"in the middle of your artful dodge your foot slips and you"print"bang your head on the metal wall and pass out."print"you wake up shortly after only to die as the Gothon stomps on"print"your head and eats you."print"death"elif action == "tell a joke":print"lucky for you they made you learn Gothon insults in the academy."print"you tell the one Gothon joke you know:"print"lbhe sa aoih aoid aiu aoih aojf aoijoafijo."print"the Gothon stops, tries not to laugh, then busts out laughing and can't move."print"while he's laughing you run up and shoot him square in the head"print"putting him down, then jump through the weapon armory door."return"laser_weapon_armory"else:print"does not compute."return"centeal_corridor"def laser_weapon_armory(self):print"you do a dove roll into the weapon armory, crouch and scan the room"print"for more Gothons that might be hiding. it's dead quiet, too quiet."print"you stand up and run to the far side of the room and find the"print"neutron bomb in the container. there's a keypad lock on the box"print"and you need the code to get the bomb out.if you get code"print"wrong 10 times then the lock closes forever an you can't"print"get the bomb.the code is 3 digits."code = "%d%d%d"%(randint(1,9),randint(1,9),randint(1,9))guess = raw_input("[keypad]>")guesses = 0while guess != code and guesses <10:print"bzzzzd"guesses += 1guess = raw_input("[keypad]>")if guess == code:print "the comntainer clicks open and the seal breaks, letting gas out."print"you grab the neutron bomb and run as fast as you can to the"print"brige where you must place it in the right spot"return"the_brige"else:print"the lock buzzes one last time and then you hear a sickening"print"melting sound as the mechanism is fused together."print"you decide to sit there, and finally the Gothons blow the"print"ship from their ship and you die."return"death"def the_bridge(self):print"you burst onto the brige with the neutron destruct bomb"print"under your arm and surprises 5 Gothons who are trying to"print"take control of the ship. each of them have an even uglier."print"clown costume than the last. they haven't pulled their"print"weapons out yet, as they see the active bomb under your"print"arm and don't want to set it off."action == input(">")if action == "throw the bomb":print"in a panic you throw the bomb at the group of Gothons"print"and make a leap for the door. right as you drop it a "print"Gothon shoots your right in the bavk killing you."print"as you die you see another Gothon frantically try to disarm"print"the bomb. you die knowing they will probably blow up when"print"it goes off."return "death"elif action == "slowly pace the bomb":print"you point your blaster at the bomb under your arm"print"and the Gothons put their hands up and start to sweat."print"you inch backward to the door, open it and then carefully"print"you jumped back the lock so the Gothons can't get out."print"now that the bomb is placed your run to escape pod to"print"get off this tin can"return"escape_pod"else:print"does not compute"return "the_bridge"def escape_pod(self):print"you rush through the ship desperately trying to make it to"print"the escape pod before the whole ship explodes. it seems like"print"hardly any Gothons are on the ship,so your run is clear of"print"interference. you get to the chamber with the escape pods, and"print"now need to pick one to take. some of them could be damaged"print"but you don't have time to look. there's 5 pods, whic one"print"do you take?"good_pod= randint(1,5)guess = raw_input("[pod]>")if int(guess) != good_pod:print"you jumped into %s and hit the eject buttom."%guessprint"the pod escapes out into the void of space, then"print"implodes as the hull ruptures, crushing your body"print"into jam jelly"return"death"else:print"you jumped into %s and hit the eject buttom."%guessprint"the pod easily slides out into the space heading to"print"the planet below. as it flies to the planet, as you look"print"back and see your ship implode then explose like a"print"bright star, taking put the Gothon ship at the same"print"time, you won!"exit(0)a_game = Game("central_corridor")
a_game.play()
  1. 对class的理解更深一层了  class像一个函数,变量(称为属性) 集合  self就是那object
  2. __init__(self)是是初始化的一种方法
  3. 每个函数起码有一个参数(self)(不知道可不可以没有)
  4. getattr(看上去就像 get attribute) 访问某个参数 直接用”xx “  就可以访问xx了不再需要字典给函数取名字了
  5. a = Game() b = Game() 与Game()不是一个东西 对Game()修改以后就都变了但现在对a修改却只有a的属性的值会变
  6. __dict__得到class中的属性与相应的值  5中 a b 与Game()的属性值是不一定不一样的 而且Game会多出几个属性(我不知道是干什么的 如__main__)

http://www.ppmy.cn/news/271815.html

相关文章

20-HTML

目录 1.概念 2.创建HTML页面 3.运行HTML程序 4.HTML结构 4.1.HTML文件基本结构 4.2.标签层次结构 PS&#xff1a;开发者工具简单介绍 PS&#xff1a;快速生成代码框架&#xff1a; 5.HTML常见标签 5.1.注释标签 5.2.标题标签&#xff1a;h1 - h6 5.3.段落标签&…

1.1 数据结构

2023/6/7 1.1数据结构 数据元素是数据的基本单位。一个数据元素由若干个数据项组成。数据项又分为简单数据项及符合数据项两种。简单数据项不能再分割&#xff0c;符合数据项由若干个数据项组成。 类型是一组值的集合。抽象数据类型&#xff08;ADT&#xff09;用一种类型和…

你懂什么是闭包吗-看场景学懂闭包

闭包是什么&#xff1f; 闭包是指有权访问另一个函数作用域中变量的函数。 当某个函数被调用时&#xff0c;会创建一个作用域和相应的作用域链&#xff0c;然后使用arguments和其他命名参数的值来初始化函数的活动对象。 从内到外函数的活动对象直至全局执行环境的变量对象形…

分享可以在线录音实时转写的方法

小伙伴们使用过录音记录吗&#xff1f;那知道录音实时转写吗&#xff1f;有没有听说过这个功能呢&#xff1f;它是可以通过语音识别技术&#xff0c;将录音中的信息快速转换为文本&#xff0c;并实现实时显示输出的功能。听起来是不是很有趣&#xff1f;而且它无需任何专业设备…

android6.0原生壁纸,惊呆了!安卓6.0壁纸竟然是这样得来的

原标题&#xff1a;惊呆了&#xff01;安卓6.0壁纸竟然是这样得来的 摘要www.hiapk.com 谷歌原生的壁纸一直受到“原生党”的青睐&#xff0c;简约好看。此次&#xff0c;谷歌Android 6.0中一共内置了九张壁纸&#xff0c;三张是谷歌地球拍摄的&#xff0c;三张是摄影师拍摄的风…

科学计算机壁纸,科幻题材电脑插画桌面壁纸

科学幻想(science fiction)简称科幻(sci-fi)。科幻小说自诞生以来&#xff0c;曾有过scientific romance、science fantasy、off-trail story 、different story、impossible story、scientifiction、astounding story等众多名目&#xff0c;而最终被确定为science fiction。 “…

自己的linux桌面背景,动手创建属于自己的 Ubuntu 20.04 LTS 个性化艺术壁纸

我们现在可以为Ubuntu 20.04创建壁纸了吗&#xff1f;是的&#xff0c;现在你可以了。 Ubuntu的开发人员已经发布了一些材料&#xff0c;这样那些不太喜欢新墙纸的人就可以为Ubuntu创建自己喜欢的艺术墙纸了。 除了壁纸&#xff0c;还有两个.svg矢量图形格式的Ubuntu 20.04吉祥…

彩色艺术创意Mac动态壁纸

彩色艺术创意动态壁纸分享给大家&#xff0c;设计时尚新颖&#xff0c;高分辨率带给您更优质的视觉体验&#xff0c;感兴趣的来试试吧~ 彩色艺术创意动态壁纸