0

The other posts are difficult to apply to my issue.

Basically the code starts out by prompting the user to turn "right" or "left". When I choose "right" it takes me to the "cthulhu room" function and prompts me to either "flee" or get my "head" eaten. Whether I choose "flee" or "head" I always get the error in the title. What's weird is that the book I'm using has the code listed exactly like this, and I even found video on youtube with the exact same code and the guy was able to get the intened results from the "cthulhu" function.

Code:

def cthulhu_room():

    print "Here you see the great evil Cthulhu."

    print "He, it, whatever stares at you and you go insane."

    print "Do you flee for your life or eat your head?"

    next == raw_input("> ")

    if "flee" in next:

        start()

    elif "head" in next:

        dead("Well that was tasty!")

    else:

        cthulhu_room()

1 Answer 1

5

You are using == which is for comparison not assignment use = and don't use next as a variable name as it is shadowing the builtin python function next:

nxt = raw_input("> ")

if "flee" in next: raises the error you see as you have not actually assigned next to any value so you are actually trying to iterate over a reference to the builtin function next.

In [3]: next
Out[3]: <function next>

If you had next = raw_input("> ") it would work but as previously stated shadowing builtin functions is not a good idea

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you! That worked. But what do you mean by not assigning a value to next I'm therefore "trying to iterate over a reference to the builtin function next"? Also, what the in and out thing? Sorry I'm 3 months in to programming.
@staredecisis, next is a function, so if you use next without parens you get a reference to the function, you need to call a function to actually use it
but isn't it called when I use it in the "if statement" below? That is below the next = raw_input("> ")
No, you basically test if next is equal to raw_input("> "), you then try to see if the a string is in next i.e a reference to a builtin function. You don't use next = raw_input("> ") you use next == raw_input("> ")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.