源代码:
#-*-coding:utf-8-*-
from sys import exit#向sys模块借一个exit函数用来退出程序
def gold_room():# 这里的数字检测Bug太严重了
print "This room is full of gold. How much do you take?"
next = raw_input("> ")
if "0" in next or "1" in next:
how_much = int(next)
else:
dead("Man, learn to type a number.")
if how_much < 50:
print "Nice, you're not greedy, 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 honer."
print "The fat bear is in front of another door."
print "How are you going to move the bear?"
bear_moved = False
while True: #就是 while 1
next = raw_input("> ")
if next == "take honey":
dead("The bear looks at you then slaps your face off.") # slaps 拍打
elif next == "taunt bear" and not bear_moved: # taunt 嘲笑
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.")# pissed 厌烦的,恼火的
elif next == "open door" and bear_moved:
gold_room()
else:
print "I got no idea what that means."
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 your life or eat your head." # flee 逃离
next = raw_input()
if "flee" in next:
start()
elif "head" in next:
dead("Well that was tasty!")
else:
cthulhu_room()
def dead(why):
print why,"Good job!"
exit(0)
def start():
print "You are in a dark room."
print "There is a door to your right and left."
print "Which one do you take?"
next = raw_input("> ")
if next == "left":
bear_room()
elif next == "right":
cthulhu_room()
else:
dead("You stumble around the room until you starve.")
start()
运行结果
=======================================================================================================================
附加练习
1.流程图真恶心。。。建议用 visio 画
4.简化扩展?这个我倒是没想到,可能还是函数调用的问题吧
Python 里面是没有 主函数 main() 这东西的
实际上的主函数在这里就是一个 start() ?其实也算不上。。。
5.
这是一个很严重的 bug ,0 和 1的检测表示你只能输入 带有 0 和 1 的数字
如果我输入 52 ,那么会提示这不是一个数字
有没有更好的办法?
有! 使用 isdigit() 函数,这个函数可以判断数字
#-*-coding:utf-8-*-
from sys import exit#向sys模块借一个exit函数用来退出程序
def gold_room():
print "This room is full of gold. How much do you take?"
next = raw_input("> ")
if next.isdigit():
how_much = int(next)
else:
dead("Man, learn to type a number.")
if how_much < 50:
print "Nice, you're not greedy, 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 honer."
print "The fat bear is in front of another door."
print "How are you going to move the bear?"
bear_moved = False
while True: #就是 while 1
next = raw_input("> ")
if next == "take honey":
dead("The bear looks at you then slaps your face off.") # slaps 拍打
elif next == "taunt bear" and not bear_moved: # taunt 嘲笑
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.")# pissed 厌烦的,恼火的
elif next == "open door" and bear_moved:
gold_room()
else:
print "I got no idea what that means."
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 your life or eat your head." # flee 逃离
next = raw_input()
if "flee" in next:
start()
elif "head" in next:
dead("Well that was tasty!")
else:
cthulhu_room()
def dead(why):
print why,"Good job!"
exit(0)
def start():
print "You are in a dark room."
print "There is a door to your right and left."
print "Which one do you take?"
next = raw_input("> ")
if next == "left":
bear_room()
elif next == "right":
cthulhu_room()
else:
dead("You stumble around the room until you starve.")
start()
这样就搞定了!