开发者

Learn python the hard way, exercise 35

开发者 https://www.devze.com 2023-03-29 05:11 出处:网络
I\'m having some problems with exercise 35 in \"Learn python the hard way\". def bear_room(): print \"There is a bear here.\"

I'm having some problems with exercise 35 in "Learn python the hard way".

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("> ")

        if next == "take honey":
            dead("The bear looks at you then slaps your face off.")
        elif next == "taunt 开发者_JAVA技巧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 leg off.")
        elif next == "open door" and bear_moved:
            gold_room()
        else:
            print "I got no idea what that means."

a) I don't understand why the While activates. In line 6 we write that the bear_moved condition is False. So if it's False, and While activates when it's True, it shouldn't activate!

b) I also have problems with the AND operator in the IF and ELIF lines. Why are we asking if bear_moved changes in value?? No other instruction in the block affects bear_moved other than line 33 (but that line exits the while).

I'm not sure I've explaned myself, please do tell me if I haven't. And thank you for your answers, i'm a complete newbie.

http://learnpythonthehardway.org/book/ex35.html


a) The while loop does not test bear_movedit tests True which is, well, always true.

b) The difference is, you may only move the bear once - a second time would be fatal. This serves to distinguish. But I don't consider it good style - a better way would be

    elif next == "taunt bear":
        if bear_moved:
            dead("The bear gets pissed off and chews your leg off.")
        else:
            print "The bear has moved from the door. You can go through it now."
            bear_moved = True


while loop executes its body while the boolean condition is true. TRUE is always true so it is an infinite loop.


a) I don't understand why the While activates. In line 6 we write that the bear_moved condition is False. So if it's False, and While activates when it's True, it shouldn't activate!

This While True construct creates an infinite loop. This will loop until a break statement or some other control flow changing statement occurs.

b) I also have problems with the AND operator in the IF and ELIF lines. Why are we asking if bear_moved changes in value?? No other instruction in the block affects bear_moved other than line 33 (but that line exits the while).

The code you provided appears to model the following situation: The user is stuck in a room, with a bear. They need to input the correct actions to make the bear move away from the door, and then they need to exit the door.

The infinite while loop maintains that if the input the user gives does not change the control (functions dead(... or gold_room()) then the program will wait for input again.

Initially, the bear has not moved, so bear_moved is false. When the user inputs taunt bear, this will make the bear move away from the door, making bear_moved true. At this point, if the user attempts to taunt bear AGAIN, then the bear will kill them. However, if the user inputs open door after they have taunted the bear once, they will move to the next room.


a) It says While True not While bear_moved, True is a constant, so this while loop will go on forever.

b) Since the while loop goes on forever and some answers ("taunt bear") can change the value of bear_moved

Executing this function will keep asking questions until you either die or gold_room() is executed.

Edit: With die I do mean the dead() function is executed, and not that you yourself would die, I honestly hope you remain in good health while learning Python :-)


a) I don't understand why the While activates. In line 6 we write that the bear_moved condition is False. So if it's False, and While activates when it's True, it shouldn't activate!

while True just means loop indefinitely until a break.

Each time the while loop starts, it checks if True is True (and it very obviously is) so it loops forever (again, or until a break).

bear_moved is a variable that has nothing to do with the execution of the while loop.

0

精彩评论

暂无评论...
验证码 换一张
取 消