zoukankan      html  css  js  c++  java
  • Learn Python the hard way, ex35 分支和函数

     1 #!/usr/bin/python
     2 #coding:utf-8
     3 from sys import exit
     4 
     5 def gold_room():
     6     print "this room is full of gold. how much do you take?"
     7 
     8     next = raw_input(">>")
     9     if "0" in next or "1" in  next:
    10         how_much = int(next)
    11     else:
    12         dead("man, learn to type a number")
    13 
    14     if how_much < 50:
    15         print "nice,you're not greedy,you win"
    16         exit(0)
    17     else:
    18         dead("you greedy bastard")
    19 
    20 def bear_room():
    21     print "there is a bear here"
    22     print "the bear has a bunch of honey"
    23     print "the fat bear is in front of another door."
    24     print "how are you going to move the bear?"
    25     bear_moved = False
    26 
    27     while True:
    28         next = raw_input(">>")
    29 
    30         if next == "take honey":
    31             dead("the bear looks at you then slaps your face off")
    32         elif next == "taunt bear" and not bear_moved:
    33             print "the bear has moved from the door.you can go through it now"
    34             bear_moved = True
    35         elif next == "taunt bear" and bear_moved:
    36             dead("the bear gets pissed off and chews your legs off")
    37         elif next == "open door" and bear_moved:
    38             gold_room()
    39         else:
    40             print "I got no idea what that means"
    41 
    42 def cloth_room():
    43     print "here you see the great evil cthulhu"
    44     print "he , it,whatever stares at you and you go insane"
    45     print "do you flee for your life or eat your head?"
    46 
    47     next = raw_input(">>")
    48 
    49     if "flee" in next:
    50         start()
    51     elif "head" in next:
    52         dead("well that was tasty")
    53     else:
    54         cloth_room()
    55 
    56 def dead(why):
    57     print why,"good job"
    58     exit(0)
    59 
    60 def start():
    61     print "you are in a dark room"
    62     print "there is a door to your left and right"
    63     print "which one do you take?"
    64 
    65     next = raw_input(">>")
    66 
    67     if next == "left":
    68         bear_room()
    69     elif next == "right":
    70         cloth_room()
    71     else:
    72         dead("you stumble around the room until you starve")
    73 
    74 start()

     相关函数:

    Python List append()方法

  • 相关阅读:
    剑指offer-整数中1出现的次数
    剑指offer-连续子数组的最大和
    剑指offer-最小的k个数
    剑指offer-数组中超过一半的数字
    剑指offer-二叉搜索树与双向链表
    剑指offer-复杂链表的复制
    剑指offer-二叉树中和为某一值的路径
    剑指offer-二叉搜索树的后序遍历
    Alpha 冲刺 (7/10)
    Alpha 冲刺 (6/10)
  • 原文地址:https://www.cnblogs.com/sub2020/p/7856044.html
Copyright © 2011-2022 走看看