企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
# 习题 35: 分支和函数 你已经学会了 if语句、函数、还有列表。现在你要练习扭转一下思维了。把下面的代码写下来,看你是否能弄懂它实现的是什么功能。 <table class="highlighttable"><tbody><tr><td class="linenos"> <div class="linenodiv"> <pre> 1&#13; 2&#13; 3&#13; 4&#13; 5&#13; 6&#13; 7&#13; 8&#13; 9&#13; 10&#13; 11&#13; 12&#13; 13&#13; 14&#13; 15&#13; 16&#13; 17&#13; 18&#13; 19&#13; 20&#13; 21&#13; 22&#13; 23&#13; 24&#13; 25&#13; 26&#13; 27&#13; 28&#13; 29&#13; 30&#13; 31&#13; 32&#13; 33&#13; 34&#13; 35&#13; 36&#13; 37&#13; 38&#13; 39&#13; 40&#13; 41&#13; 42&#13; 43&#13; 44&#13; 45&#13; 46&#13; 47&#13; 48&#13; 49&#13; 50&#13; 51&#13; 52&#13; 53&#13; 54&#13; 55&#13; 56&#13; 57&#13; 58&#13; 59&#13; 60&#13; 61&#13; 62&#13; 63&#13; 64&#13; 65&#13; 66&#13; 67&#13; 68&#13; 69&#13; 70&#13; 71&#13; 72&#13; 73&#13; 74&#13; 75&#13; 76</pre> </div> </td> <td class="code"> <div class="highlight"> <pre>from sys import exit&#13; &#13; def gold_room():&#13; print "This room is full of gold. How much do you take?"&#13; &#13; next = raw_input("&gt; ")&#13; if "0" in next or "1" in next:&#13; how_much = int(next)&#13; else:&#13; dead("Man, learn to type a number.")&#13; &#13; if how_much &lt; 50:&#13; print "Nice, you're not greedy, you win!"&#13; exit(0)&#13; else:&#13; dead("You greedy bastard!")&#13; &#13; &#13; def bear_room():&#13; print "There is a bear here."&#13; print "The bear has a bunch of honey."&#13; print "The fat bear is in front of another door."&#13; print "How are you going to move the bear?"&#13; bear_moved = False&#13; &#13; while True:&#13; next = raw_input("&gt; ")&#13; &#13; if next == "take honey":&#13; dead("The bear looks at you then slaps your face off.")&#13; elif next == "taunt bear" and not bear_moved:&#13; print "The bear has moved from the door. You can go through it now."&#13; bear_moved = True&#13; elif next == "taunt bear" and bear_moved:&#13; dead("The bear gets pissed off and chews your leg off.")&#13; elif next == "open door" and bear_moved:&#13; gold_room()&#13; else:&#13; print "I got no idea what that means."&#13; &#13; &#13; def cthulhu_room():&#13; print "Here you see the great evil Cthulhu."&#13; print "He, it, whatever stares at you and you go insane."&#13; print "Do you flee for your life or eat your head?"&#13; &#13; next = raw_input("&gt; ")&#13; &#13; if "flee" in next:&#13; start()&#13; elif "head" in next:&#13; dead("Well that was tasty!")&#13; else:&#13; cthulhu_room()&#13; &#13; &#13; def dead(why):&#13; print why, "Good job!"&#13; exit(0)&#13; &#13; def start():&#13; print "You are in a dark room."&#13; print "There is a door to your right and left."&#13; print "Which one do you take?"&#13; &#13; next = raw_input("&gt; ")&#13; &#13; if next == "left":&#13; bear_room()&#13; elif next == "right":&#13; cthulhu_room()&#13; else:&#13; dead("You stumble around the room until you starve.")&#13; &#13; &#13; start()&#13; </pre> </div> </td> </tr></tbody></table> ### 你应该看到的结果 下面是我玩游戏的过程: ~~~ $ python ex35.py You are in a dark room. There is a door to your right and left. Which one do you take? > left There is a bear here. The bear has a bunch of honey. The fat bear is in front of another door. How are you going to move the bear? > taunt bear The bear has moved from the door. You can go through it now. > open door This room is full of gold. How much do you take? > asf Man, learn to type a number. Good job! $ ~~~ ### 加分习题 1. 把这个游戏的地图画出来,把自己的路线也画出来。 1. 改正你所有的错误,包括拼写错误。 1. 为你不懂的函数写注解。记得文档注解该怎么写吗? 1. 为游戏添加更多元素。通过怎样的方式可以简化并且扩展游戏的功能呢? 1. 这个 gold_room 游戏使用了奇怪的方式让你键入一个数字。这种方式会导致什么样的 bug? 你可以用比检查 0、1 更好的方式判断输入是否是数字吗?int() 这个函数可以给你一些头绪。