循环学习Python艰苦之路练习36

时间:2014-05-29 18:07:03

标签: python while-loop

dragon_room中的while循环没有运行,我不知道为什么。我得到'>>'一遍又一遍地提示,程序永远不会退出或带我到另一个房间。

from sys import exit

def food_room():
    print "This room is full of candy bars.  How many do you take?"

    next = raw_input("gold room> ")
    if "0" in next or "1" in next:
        how_much = int(next)
    else:
        dead("type a number.")

    if how_much < 5:
        dead("You've starved, sorry")
    else:
        print "You've survived! congratulations."
        exit(0)


def bear_room():
    print "There is a bear here."
    print "The bear has a bunch of honey."
    print "The fat bear is in front of another door."
    print "How are you going to move the bear?"
    bear_moved = False

    while True:
        next = raw_input("bear room >")

        if next == "take honey":
            dead("Apparently the bear is pretty protective of his honey")
        elif next == "taunt bear" and not bear_moved:
            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 face off.")
        elif next == "open door" and bear_moved:
            food_room()
        else:
            "no idea what that means."

def dragon_room():
     print "You've opened the door on a giant, fire-breathing dragon."
     print "Do you flee or fight?"
     dragon_moved = False

     while True:
        next = raw_input(">>")

     if "flee" in next:
          start()
     elif next == "fight" and not dragon_moved:
          print "You have slayed the dragon!  There is a door behind him! you should open the door."
          dragon_moved = True
     elif next == "fight" and dragon_moved:
         dead("killed")
     elif next == "open door" and dragon_moved:
         food_room()
     else:
          dead("you're not very good at following instructions.")

def dead(why):
    print why, "good job!"
    exit(0)

def start():
    print "You are starving and 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":
        dragon_room()
    else: 
        dead("you starve.")


start()

3 个答案:

答案 0 :(得分:2)

看起来你有缩进错误:

 while True:
    next = raw_input(">>")
 ## the loop above runs forever -- you probably want to indent all of the 
 ## below code to be inside the loop.

 if "flee" in next:
      start()
 elif next == "fight" and not dragon_moved:
      print "You have slayed the dragon!  There is a door behind him! you should open the door."
      dragon_moved = True
 elif next == "fight" and dragon_moved:
     dead("killed")
 elif next == "open door" and dragon_moved:
     food_room()
 else:
      dead("you're not very good at following instructions.")

答案 1 :(得分:2)

你在dragon_room中缩进不正确。具体做法是:

while True:
    next = raw_input(">>")

它将永远得到一个新的next,永远不会运行其余的。

将剩余的函数缩进到另一个标签中:

 while True:
    next = raw_input(">>")

    if "flee" in next:
        start()
    elif next == "fight" and not dragon_moved:
        print "You have slayed the dragon!  There is a door behind him! you should open the door."
        dragon_moved = True
    elif next == "fight" and dragon_moved:
        dead("killed")
    elif next == "open door" and dragon_moved:
        food_room()
    else:
        dead("you're not very good at following instructions.")

答案 2 :(得分:0)

这是因为你的缩进是关闭的。你的条件在while循环之外,所以while循环中唯一的东西就是输入语句。