I have been making an Adventure Game and when I try to test it, it always comes up with

Hello! Welcome to the adventure game!
What is your name? Parsia
Parsia? Well hello there!
Let us begin...
You are at a trail with two paths. Left or right? left
Traceback (most recent call last):
  File "/Users/mhedayat/Desktop/Dropbox/AdventureGame.py", line 8, in <module>
    if partone == left:
NameError: name 'left' is not defined

Here is the code:

print "Hello! Welcome to the adventure game!"
name = raw_input("What is your name? ")
print name + "? Well hello there!"
print "Let us begin..."
partone = raw_input("You are at a trail with two paths. Left or right? ")
if partone == left:
    print "You are now heading toward a town! You have survived! [Move on]"
elif partone == right:
    print "You accidently fall into a sandpit and die! You lose. [Exit Game]"
    print "Not a choice!"
parttwo = raw_input("You are now in a village. A farmer offers you a beet or a bubbling potion. Which one?")
if parttwo == potion:
    print "You now have cancer and will die in a couple minutes. You lose. [Exit Game]"
elif parttwo == beet:
    print "You eat the beet and grow close to the farmer. You get to stay the night with him. You survived! [Move on]"
    print "Not a choice!"
partthree = raw_input("You're at the farmers house and he lets you stay in 1 of 2 rooms. First or second?")
if partthree == first:
    print "You walk into the room and see nothing but the farmer and a girl with pigtails. You are probably screwed. [Move on]"
elif partthree == second:
    print "This room has a nice bed and nightstand. You are able to sleep the night! [Move on]"
    print "Not a choice!"

Please help me figure this out. Thank you.

4 Years
Discussion Span
Last Post by vegaseat

At line 8, python thinks that a variable left is used, which was not previously defined. You probably meant

if partone == 'left':

Here 'left' is a constant string instead of a variable name.

Edited by Gribouillis


@Gribouillis Thank you so much. That worked! Now, is there a way for the python code to stop if they pick the wrong answer?


is there a way for the python code to stop if they pick the wrong answer?

You can exit your program at any time with

import sys

The value 0 means to the OS that your program executed normally. You may exit with another value to indicate an error.


Well, you could just simply return from that function:

def f():
    i=raw_input(": ") #i=input(": ")
    if (i=="1"):
        print "Hello!" #print ("Hello!")

Edited by Lucaci Andrew

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.