0

私は Zed Shaw の Learn Python The Hard Way を使って独学していますが、彼が私たちに独自のテキスト アドベンチャー ゲームを作成するように指示する追加のクレジット演習に問題があります。

このコードを数時間で完成させたときは、かなりうまくやっていると思いましたが、残念なことに、最初の遭遇を何度も実行し続けています。私の脳は焼き尽くされ、なぜ最初の出会いが起こり続けるのか理解できません. たぶん、皆さんは私が見ていないものを見ていますか?

最後に、このコードを正しく順序どおりに動作させるにはどうすればよいですか?

# First Encounter (main program really)
def fi_en():
    print"""
It smells of damp vegetation, and the air is particularly thick. You can 
hear some small animals in the distance. This was a nice place to sleep.

1. Stay close, find some cover, and wait for food to show up.

2. Explore the nearby marsh & find the nearest river, following it downstream."

3. Walk towards the large mysterious mountain in the distance. 
"""
    answer = raw_input(prompt)
    if answer == 1:
        cun_one = roll_3d6()
        if cun_one <= cun - 2:
            print"""Time passes as eventually you capture some varmints.
You feel slightly more roguish."""
            cun = cun + 1
            fi_en()
        else: 
            print """Time passes and a group of slavers marches into right 
where you are hiding in the woods. They locate you, capture you, and haul you
away for a lifetime of servitude in the main city.
Goodbye %s""" % name
    elif answer == 2: 
        power = roll_3d6()
        if power <= pow -4:
            print"""You trudge through the marshes until you eventually reach 
a large river. Downstream from the river is a large temple covered in vines,
you walk towards it. You feel more powerful."""
            pow = pow + 2
            te_en()
        else:
            print """The vegetation here wraps itself around your legs making
it impossible to move. You will most likely die here in the vegetation. 
Goodbye %s.""" % name
    elif answer == 3:
        cun_two = roll_3d6()
        if cun_two <= cun:
            print """You make your way towards the mountain and you encounter
a really large group of devil dogs guarding the entrance to the mountain."""
            dd_en()
    else: 
        print"You have gotten lost and ended up right where you started."
        fi_en()




######################## MAIN PROGRAM ###################################################
prompt = "> "

print "\n\t\tWelcome to LeGeNeD oF DoObIeUs..."

print "\n\t\tProgrammed using Python 2.7 by Ray Weiss"

name = raw_input("\nWhat is your name brave rogue? > ")

print "\nOk %s, lets roll up some character stats." % name
print "\nPress enter to roll 3D6 for each stat."
raw_input()

display_stats()

print "\nThese are your stats. They can be changed by things in game."

print "\nThese stats will have an effect on the outcomes that you choose."

print "\nYou will be presented with many choices, type in a number."

print "\nOr risk typing something different."

print "\nPress enter to start the game"
raw_input()

print"""You are Doobieus, you are but a wandering rogue in the vast, 
mysterious, and deadly land of Calgaria. Your goal is to survive. 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
You have just woken up in the woods, its really cold, and you are hungry."""

fi_en()

ここに私の出力があります

You have just woken up in the woods, its really cold, and you are hungry.

It smells of damp vegetation, and the air is particularly thick. You can 
hear some small animals in the distance. This was a nice place to sleep.

1. Stay close, find some cover, and wait for food to show up.

2. Explore the nearby marsh & find the nearest river, following it downstream."

3. Walk towards the large mysterious mountain in the distance. 

> 2
You have gotten lost and ended up right where you started.

It smells of damp vegetation, and the air is particularly thick. You can 
hear some small animals in the distance. This was a nice place to sleep.

1. Stay close, find some cover, and wait for food to show up.

2. Explore the nearby marsh & find the nearest river, following it downstream."

3. Walk towards the large mysterious mountain in the distance. 

>
4

1 に答える 1

3

これを試して:

print '2' == 2

それを得る?

追加するために編集:

OPへのコメント投稿者が指摘したように、問題はraw_inputが文字列を返し、すべての応答を整数として比較していることです。

回答を int に変更することもできますが、わざわざする必要はありません。コードを変更して、答えが特定の文字列であるかどうかを確認してみませんか?

于 2012-09-28T14:03:18.363 に答える