-4

我正在学习 Python,下面是一个包含 while 循环的 Python 游戏函数,但是这个循环作为一个无限循环工作,我无法理解它是如何工作的以及它应该如何工作。

谁能帮我解释一下while循环的含义,以及“bear_move=False”变量和循环条件“WHILE TRUE”之间的关系。我无法理解这个 while 循环结构和条件,以及这个 while 循环条件与什么相关。

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:            #What is this while loop means here
        next=raw_input("You should write take honey or taunt bear: >")

    if next=="take honey":
        print "The bear looks at you then pimp slaps your face off."
    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:
        print "The bear gets pissed off and chews your crotch off."
    elif next=="open door" and bear_moved:
        gold_room()
    else:
        print "I got not I dea what that means."
4

2 回答 2

3

if在循环之外,在 python 中,缩进很重要:

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:            #What is this while loop means here
        next=raw_input("You should write take honey or taunt bear: >")

    # here you have to move all your block to the 
    # right, so it would be inside the while loop

        if next=="take honey":
            print "The bear looks at you then pimp slaps your face off."
        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:
            print "The bear gets pissed off and chews your crotch off."
        elif next=="open door" and bear_moved:
            gold_room()
        else:
            print "I got not I dea what that means."

基本上它的作用是在无限循环中读取输入并根据该动作执行一些动作。您可以将exit命令添加到循环中,以便随时退出:

        if next=="take honey":
            print "The bear looks at you then pimp slaps your face off."
        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:
            print "The bear gets pissed off and chews your crotch off."
        elif next=="open door" and bear_moved:
            gold_room()
        elif next=="exit":
            break        # exit from cycle
        else:
            print "I got not I dea what that means."
于 2013-09-12T08:01:56.653 回答
1
  while True:            #What is this while loop means here

while True是一个无限循环。这些循环经常被使用。如果您编写这样的循环,您必须在每次迭代中检查游戏是否已达到手动停止的条件。您可以使用关键字从内部停止循环break。有关python 中控制流的更多详细信息,请参阅文档

在您的代码中,您可能想在这一点上休息一下:

elif next=="open door" and bear_moved:
    gold_room()
    break

因此,如果门打开并且熊已经移动,则循环将结束。

于 2013-09-12T08:26:05.470 回答