-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 に答える