在python中分支对话

时间:2015-11-19 17:59:38

标签: python

我在业余时间自学python。我只是在这里工作了几天所以为了练习我一直在搞乱各种raw_input线。目前我正试图为一位朋友做一个简短的文本冒险尝试。我遇到了一个障碍,其中一部分询问用户是否想进入一个村庄。如果他们选择进入,一切都运转正常,但如果他们选择不这样做,我需要它跳过村内的所有对话和输入。如何让它跳到页面下方的输入?

它非常基本但是在这里:

name = raw_input("What is your name adventurer? ")
where = raw_input("You are in a magical forest, you can go North or East, which 
way do you go?")
if where.lower() == "north":
    troll = raw_input("You encounter a troll, what do you do? Attack or flee? ")
else:
    print("You encounter a dragon, you are dead, GAME OVER")
if troll.lower() == "attack":
    print ("You have no weapon, that was a bad choice, you are dead, GAME OVER")
else:
    flee = raw_input("You flee further North, you encounter a village, do you 
enter or continue? ")
if flee.lower() == "enter":
    army = raw_input("As you enter the village, a knight hands you a sword, 
assuming you are there to join the army. Join him or leave the village? ")
else:
    print ("You ignore the village and continue down the road.") #jump from here
if army.lower() == "join":
    print ("You head off to war and are killed almost instantly because you are an 
untrained fool")
else:
    print ("You are kicked out of the village")
dark = raw_input("It is getting dark, do you continue or seek shelter") #to here
if dark.lower() == "continue":
    print ("You are attacked by vampires and killed, GAME OVER")
else:
    caves = raw_input("You find two caves, do you pick cave one which is pitch 
black, or two which has light coming from it? ")
if caves.lower() == "one":
    print ("You enter the cave, light a small fire, and sleep for the night.")
else:
    print ("You enter the cave, there are bandits inside, you are murdered, GAME OVER")

我想在第一个粗线后跳到黑暗。如果发现其他任何可以修复或改进的内容,请说出来。所有的批评都是受欢迎的。

1 个答案:

答案 0 :(得分:0)

更改此块:

if flee.lower() == "enter":
    army = raw_input("As you enter the village, a knight hands you a sword, 
assuming you are there to join the army. Join him or leave the village? ")
else:
    print ("You ignore the village and continue down the road.") #jump from here
if army.lower() == "join":
    print ("You head off to war and are killed almost instantly because you are an 
untrained fool")
else:
    print ("You are kicked out of the village")
dark = raw_input("It is getting dark, do you continue or seek shelter")

进入这个区块:

if flee.lower() == "enter":
    army = raw_input("As you enter the village, a knight hands you a sword, 
assuming you are there to join the army. Join him or leave the village? ")
    if army.lower() == "join":
        print ("You head off to war and are killed almost instantly because you are an 
untrained fool")
    else:
        print ("You are kicked out of the village")
else:
    print ("You ignore the village and continue down the road.")
dark = raw_input("It is getting dark, do you continue or seek shelter")

这会将army输入移动到enter块的内部,这样只有当它进入该语句时才会执行。

一些注意事项: 你可能想把它全部放在main()函数中,并在你死的时候调用return,这样它就不会继续了。如果你不想使用某个函数,另一个选择是将所有内容放在try块中,如果你死了就调用raise,然后用except块处理死亡。你说你是初学者,所以你可能还没有学过这些东西,但这肯定是值得研究的东西