我正在制作一个“选择您自己的文本冒险”游戏,到目前为止,该游戏包含太多嵌套。示例代码如下 = 我的代码格式相同,只是嵌套更多 - 有时接近 10 层深。
我的问题是:有什么办法可以把它压平吗?这些字符串意味着我需要每个 IF 语句在每次有效时打印一些内容,所以我不能只使用“AND”,如下所示:
if A and B:
do something
elif C and D:
do something else
Run Code Online (Sandbox Code Playgroud)
我想过将重复的部分放在自己的函数中,但是在这种情况下这不会提高可读性 - 至少我无法弄清楚。
帮助?
print "First String"
choice = raw_input("Choose A or B")
if choice == "A":
print "You Chose A"
choice = raw_input("Choose C or D")
if choice == "C":
print "You Chose C"
choice = raw_input("Choose E or F")
if choice == "E" or choice == "F":
print "END"
elif choice == "D":
print "You …Run Code Online (Sandbox Code Playgroud)