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!