Question

Im teaching myself using Zed Shaw's Learn Python The Hard Way, and I'm having trouble with an extra credit exercise where he has us making our own text adventure game.

I thought I was doing pretty well when I finished this code in a few hours, but to my dismay it keeps running the first encounter over and over again. My brain is fried and I cant figure out why the first encounter keeps occurring. Maybe you guys see something I dont?

In closing, how can I get this code to work correctly and in order?

# 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()

Here is my output

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. 

>
Was it helpful?

Solution

Try this:

print '2' == 2

Get it?

EDITTED TO ADD:

A slightly less snarky answer -- as noted by a commenter to the OP, the issue is that raw_input returns a string and you are comparing all of your responses as integers.

You could change answer to an int, but why bother? Why not just change your code so your are checking to see if the answer is a specific string?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top