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 回答