Nâng cấp code bài 35 trong sách Learn Python the hard way để chơi lại game

Dưới đây là đoạn code trong bài 35 sách Learn Python the hard way:

from sys import exit

def gold_room():
    print "This room is full of gold. How much do you take?"

    choice = raw_input("> ")

    if "0" in choice or "1" in choice:
        how_much = int(choice)
    else:
        dead("Man, learn to type a number")

    if how_much < 50:
        print "Nice, you win!"
        exit(0)
    else:
        dead("You greedy bastard!")
             
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:
        choice = raw_input("> ")

        if choice == "take honey":
            dead("The bear looks at you then slaps your face off.")
        elif choice == "taunt bear" and not bear_moved:
            print "The bear has moved from the door. You can go through"
            bear_moved = True
        elif choice == "taunt bear" and bear_moved:
            dead("The bear gets pissed off and chews your leg off.")
        elif choice == "go door" and bear_moved:
            gold_room()
        else:
            print "I got no idea."

def cthulhu_room():
    print "Here you see the great evil Cthulhu."
    print "He, it, whatever stares at you and you go insane"
    print "Do you flee for your life or eat your head?"

    choice = raw_input("> ")

    if "flee" in choice:
        start()
    elif "head" in choice:
        dead("Well that was tasty!")
    else:
        cthulhu_room()

def dead(why):
    print why, "Good job!"
    exit(0)
                 
def start():
    print "You're in a dark room"
    print "There is a door to your right and left."
    print "Which one do you take?"

    choice = raw_input("> ")

    if choice == "right":
        cthulhu_room()
    elif choice == "left":
        bear_room()
    else:
        dead("You stumble around the room until you starve")
start()

code này đã chạy ngon, em không có thắc mắc gì. Tuy nhiên là em có học mót được 1 đoạn code để Chơi lại muốn cho vào đây để sau khi game kết thúc là có thể tiếp tục lựa chọn Yes/No để chơi lại. Nhưng hình như cái lệnh exit(0) nó đã dừng game trước khi thực hiện được đoạn code chơi lại em viết thêm.
Em muốn bổ sung đoạn chơi lại như sau:

choi_lai = "yes"
while choi_lai == "yes":
    start()
    print"Choi lai (yes/no)?"
    choi_lai = raw_input("> ")

Nhờ các cao nhân chỉ cho em xem nên làm thế nào ạ! Em xin cảm ơn!

Thay vì exit(0) ở hàm dead, bạn bỏ nó đi, thay bằng

def dead(why):
    print("something")

def start():
...
else:
    dead("")
    return None #thoát khỏi hàm start

Còn chơi lại thì:

start() # chơi 1 lần đã
# lắp đoạn code chơi lại của bạn vào đây
1 Like

Cám ơn bác. Em đã sửa lại như bác giải thích. Đoạn từ đầu đến hàm def dead(why) em giữ nguyên. Đoạn từ sau hàm def dead(why) em đã sửa lại thế này ạ:

def dead(why):
    print why, "Good job!"
    
                 
def start():
    print "You're in a dark room"
    print "There is a door to your right and left."
    print "Which one do you take?"

    choice = raw_input("> ")

    if choice == "right":
        cthulhu_room()
    elif choice == "left":
        bear_room()
    else:
        dead("You stumble around the room until you starve")
        return None

choi_lai = "co"
while choi_lai == "co":

    start()

    print "Ban co muon choi lai khong? (Y/N)"
    choi_lai = raw_input("> ")

Tuy nhiên khi thực thi hàm start() thì đúng là khi em đánh từ gì đó bất kì để nó xảy ra lệnh else ấy ạ thì sau đó nó có thực thi đoạn chơi lại em viết thêm.
Còn trường hợp mà em lựa chọn “left” để đi đến hàm def bear_room() hoặc “right” dể đi đến hàm def cthulhu_room() thì máy nó cứ chạy loanh quanh trong các hàm này mãi mà ko thể thực thi xuống hàm chơi lại. Bác có cao kiến gì góp ý để em sửa thêm ạ.

Khi vào cthulhu_room bạn thử gõ vào "head" xem :smiley:

def cthulhu_room():
    print "Here you see the great evil Cthulhu."
    print "He, it, whatever stares at you and you go insane"
    print "Do you flee for your life or eat your head?"

    choice = raw_input("&gt; ")

    if "flee" in choice:
        start()
    elif "head" in choice:
        dead("Well that was tasty!")
    else:
        cthulhu_room()

Còn bear_room thì gõ "take honey"

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:
        choice = raw_input("> ")

        if choice == "take honey":
            dead("The bear looks at you then slaps your face off.")
        elif choice == "taunt bear" and not bear_moved:
            print "The bear has moved from the door. You can go through"
            bear_moved = True
        elif choice == "taunt bear" and bear_moved:
            dead("The bear gets pissed off and chews your leg off.")
        elif choice == "go door" and bear_moved:
            gold_room()
        else:
            print "I got no idea."
2 Likes

Nếu như đoạn code em đã sửa theo bác HK boy ở trên thì khi gõ head trong hàm cthulhu_room thì nó có xuống hàm chơi lại. Còn gõ take honey trong hàm bear_room thì nó cứ tiếp tục hỏi tiếp mà không xuống hàm chơi lại bác ạ. :frowning:

bạn copy source code lên đây được không? :smile:

Source code của bài khi chưa có code chơi lại thì em đã post ở đầu post này, còn code sau khi sửa theo bác HK boy (có bổ sung code chơi lại) thì đây ạ.

from sys import exit

def gold_room():
    print "This room is full of gold. How much do you take?"

    choice = raw_input("> ")

    if "0" in choice or "1" in choice:
        how_much = int(choice)
    else:
        dead("Man, learn to type a number")

    if how_much < 50:
        print "Nice, you win!"
        exit(0)
    else:
        dead("You greedy bastard!")
             
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:
        choice = raw_input("> ")

        if choice == "take honey":
            dead("The bear looks at you then slaps your face off.")
        elif choice == "taunt bear" and not bear_moved:
            print "The bear has moved from the door. You can go through"
            bear_moved = True
        elif choice == "taunt bear" and bear_moved:
            dead("The bear gets pissed off and chews your leg off.")
        elif choice == "go door" and bear_moved:
            gold_room()
        else:
            print "I got no idea."

def cthulhu_room():
    print "Here you see the great evil Cthulhu."
    print "He, it, whatever stares at you and you go insane"
    print "Do you flee for your life or eat your head?"

    choice = raw_input("> ")

    if "flee" in choice:
        start()
    elif "head" in choice:
        dead("Well that was tasty!")
    else:
        cthulhu_room()

def dead(why):
    print why, "Good job!"
    
                 
def start():
    print "You're in a dark room"
    print "There is a door to your right and left."
    print "Which one do you take?"

    choice = raw_input("> ")

    if choice == "right":
        cthulhu_room()
    elif choice == "left":
        bear_room()
    else:
        dead("You stumble around the room until you starve")
        return None

choi_lai = "co"
while choi_lai == "co":

    start()

    print "Ban co muon choi lai khong? (Y/N)"
    choi_lai = raw_input("> ")

Mình thấy được error rồi :smile:
Khi bạn chọn "take honey" function dead() sẽ run. Sau khi run xong nó sẽ tiếp tục vòng lặp chứ không hề chạy ra function start().
Mình nghĩ bạn nên

while True:
        choice = raw_input("> ")

        if choice == "take honey":
            dead("The bear looks at you then slaps your face off.")
        elif choice == "taunt bear" and not bear_moved:
            print "The bear has moved from the door. You can go through"
            bear_moved = True
        elif choice == "taunt bear" and bear_moved:
            dead("The bear gets pissed off and chews your leg off.")
        elif choice == "go door" and bear_moved:
            gold_room()
        else:
            print "I got no idea."

Còn ở function cthulu_room() thì không có loop

    if "flee" in choice:
        start()
    elif "head" in choice:
        dead("Well that was tasty!")
    else:
        cthulhu_room()

Như bạn thấy nếu nhập vào "flee" thì restart lại function start() còn else thì sẽ chạy restart lại function. Riêng vì sao "head" lại nhảy ra function start() lại từ đầu vì sau khi chạy xong function dead() nó sẽ kết thúc function cthulhu_room() => kết thúc luôn function start(). Lại trở về vòng lặp "Bạn có muốn chơi tiếp không?"

2 Likes

Vâng cám ơn bác. Bác chỉ giáo em thấy cũng có hiểu và cũng biết là nó đang mắc cái lặp ở trong hàm bear_room. Nhưng chưa biết giải quyết thế nào. Mong bác chỉ giao thêm.

Bạn kết thúc vòng lặp bằng cách break

>>> i = 0
>>> while i < 10:
...     print('i = ', i)
...     if i == 5:
...         break
...
0
1
2
3
4
5
2 Likes

Cám ơn bác. Em xử lý được rồi ạ. :smiley:

83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?