习题35 分支和函数


源代码:

#-*-coding:utf-8-*-
from sys import exit#向sys模块借一个exit函数用来退出程序

def gold_room():# 这里的数字检测Bug太严重了
    print "This room is full of gold. How much do you take?"
    
    next =  raw_input("> ")
    if "0" in next or "1" in next:
        how_much = int(next)
    else:
        dead("Man, learn to type a number.")
        
    if how_much < 50:
        print "Nice, you're not greedy, you win!"
        exit(0)
    else:
        dead("You greedy bastard!")

def bear_room():
    print "There is a bear here."
    print "The bear has a bunch of honer."
    print "The fat bear is in front of another door."
    print "How are you going to move the bear?"
    bear_moved = False
    
    while True: #就是 while 1
        next = raw_input("> ")
        
        if next == "take honey":
           dead("The bear looks at you then slaps your face off.") # slaps 拍打
        elif next == "taunt bear" and not bear_moved: # taunt 嘲笑
            print "The bear has moved from the door. You can go through it now."
            bear_moved = True
        elif next == "taunt bear" and bear_moved:
            dead("The bear gets pissed off and chews your leg off.")# pissed 厌烦的,恼火的
        elif next == "open door" and bear_moved:
            gold_room()
        else:
            print "I got no idea what that means."
            
            
def cthulhu_room():
    print "Here you see the great evil Cthulhu."
    print "He, it ,whatever stares at you and you go insane."
    print "Do you flee your life or eat your head." # flee 逃离
    
    next = raw_input()
    
    if "flee" in next:
        start()
    elif "head" in next:
        dead("Well that was tasty!")
    else:
        cthulhu_room()
      
        
def dead(why):
    print why,"Good job!"
    exit(0)

def start():
    print "You are in a dark room."
    print "There is a door to your right and left."
    print "Which one do you take?"
    
    next = raw_input("> ")
    
    if next == "left":
        bear_room()
    elif next == "right":
        cthulhu_room()
    else:
        dead("You stumble around the room until you starve.")
        
          
start()  

运行结果


=======================================================================================================================

附加练习

1.流程图真恶心。。。建议用 visio 画


4.简化扩展?这个我倒是没想到,可能还是函数调用的问题吧

  Python  里面是没有 主函数 main() 这东西的

  实际上的主函数在这里就是一个 start() ?其实也算不上。。。


5.

这是一个很严重的 bug ,0 和 1的检测表示你只能输入 带有 0 和 1 的数字

如果我输入 52  ,那么会提示这不是一个数字

有没有更好的办法?

有! 使用 isdigit() 函数,这个函数可以判断数字

#-*-coding:utf-8-*-
from sys import exit#向sys模块借一个exit函数用来退出程序

def gold_room(): 
    print "This room is full of gold. How much do you take?"
    
    next =  raw_input("> ")
    if next.isdigit():
        how_much = int(next)
    else:
        dead("Man, learn to type a number.")
        
    if how_much < 50:
        print "Nice, you're not greedy, you win!"
        exit(0)
    else:
        dead("You greedy bastard!")

def bear_room():
    print "There is a bear here."
    print "The bear has a bunch of honer."
    print "The fat bear is in front of another door."
    print "How are you going to move the bear?"
    bear_moved = False
    
    while True: #就是 while 1
        next = raw_input("> ")
        
        if next == "take honey":
           dead("The bear looks at you then slaps your face off.") # slaps 拍打
        elif next == "taunt bear" and not bear_moved: # taunt 嘲笑
            print "The bear has moved from the door. You can go through it now."
            bear_moved = True
        elif next == "taunt bear" and bear_moved:
            dead("The bear gets pissed off and chews your leg off.")# pissed 厌烦的,恼火的
        elif next == "open door" and bear_moved:
            gold_room()
        else:
            print "I got no idea what that means."
            
            
def cthulhu_room():
    print "Here you see the great evil Cthulhu."
    print "He, it ,whatever stares at you and you go insane."
    print "Do you flee your life or eat your head." # flee 逃离
    
    next = raw_input()
    
    if "flee" in next:
        start()
    elif "head" in next:
        dead("Well that was tasty!")
    else:
        cthulhu_room()
      
        
def dead(why):
    print why,"Good job!"
    exit(0)

def start():
    print "You are in a dark room."
    print "There is a door to your right and left."
    print "Which one do you take?"
    
    next = raw_input("> ")
    
    if next == "left":
        bear_room()
    elif next == "right":
        cthulhu_room()
    else:
        dead("You stumble around the room until you starve.")
        
          
start()  

这样就搞定了!





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 《复变函数简明教程pdf》是一本介绍复变函数理论的教材。复变函数是数学中的一门重要分支,是研究复数域上的函数性质的学科,广泛应用于数学、物理学、工程学等领域。 《复变函数简明教程pdf》从基础概念入手,介绍了复变函数的定义、性质、导数、积分等知识点。其中包括复数的极坐标表示、柯西黎曼方程、洛朗级数表达式、幂级数展开等重要内容。同时,此书也对不定积分、留数定理、解析函数、调和函数等进行了解释和讲解。 此外,《复变函数简明教程pdf》还讲述了一些具体的应用,如调和函数性质、调和函数离散化、柯西公式及其应用等等。这些实际应用不仅加深了对复变函数理论的理解,同时也展示了复变函数在实际问题解决中的威力和实用性。 总的来说,《复变函数简明教程pdf》清晰地解释了复变函数理论的基本知识,是一本适合初学者入门的教材。此外,它也为后续深入学习打下坚实的基础。 ### 回答2: 复变函数简明教程pdf是一本关于复变函数的入门教材。该书通过图示、例题和证明等多种方式形象生动地解释了复变函数的概念、性质和应用。 教材首先介绍了复数及其运算规则,然后引入了复变函数的概念和复平面,解释了复数的几何意义,特别是在不同区域的变化。随后,教材详细介绍了复变函数的基本性质,如解析性、洛朗展开等,引导读者掌握基本的计算技能。 教材进一步讲解了多项式、有理函数、三角函数和指数函数等常见的复变函数及其性质,强调了复变函数的导数和积分的重要性和使用方法。特别是针对常见的解析函数,教材结合具体的例题和练习题,帮助读者深入理解和熟练掌握。 此外,教材还介绍了复变函数的应用,如调和函数、调和共形映射等,向读者展示了复变函数在其他学科中的丰富应用。最后,教材还指导读者阅读更深入的复变函数教材和相关文献,为进一步学习和研究打下了坚实的基础。 总之,复变函数简明教程pdf通俗易懂、内容丰富、理论与实践相结合,是一本初学者不可多得的优秀教材。它能够帮助读者迅速基础掌握复变函数的基本概念和基本技能,为深入学习奠定基础。 ### 回答3: 《复变函数简明教程pdf》是一本关于复变函数的教材,阐述了复变函数的基本定义、性质与应用。该教程涉及的内容涵盖了复数的基本概念、复函数的导数和积分、复数的级数与幂级数展开、解析函数、调和函数、全纯函数与亚纯函数等。 该教程具有简明易懂的特点,旨在为读者打下扎实的基本功,并能够理解复变函数相关的高级内容。教程中逐步介绍了复数的各种运算法则和性质,帮助读者深入理解复数的本质和特点。同时,该书还讲解了复函数的基本概念以及导数和积分的概念,对于深入理解复函数的特点和性质是非常重要的。 此外,《复变函数简明教程pdf》还重点介绍了解析函数和调和函数的概念和性质,对于理解实变函数和复变函数的不同点和联系有较大的帮助。在全纯函数与亚纯函数方面,该书深入阐述了复函数的独特性质,让读者明白它们的定义和特点。 总之,《复变函数简明教程pdf》是一本学习复变函数的入门教材,读者只需要基本掌握数学基础知识即可。此外,作者还提供了相关的许多例题和习题,让读者通过练习来加深对教材的理解和应用。对于学习和研究复变函数的理论和方法的数学专业学生、研究人员以及对数学深入了解的爱好者而言,这是一本不可缺少的必备读物。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值