1.11M Members

Syntax Error??

 
0
 

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? ")
partone.lower()
str(partone)
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]"
else:
    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?")
parttwo.lower()
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]"
else:
    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?")
partthree.lower()
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]"
else:
    print "Not a choice!"

Please help me figure this out. Thank you.

 
0
 

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.

 
0
 

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

 
0
 

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
sys.exit(0)

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

 
0
 

Well, you could just simply return from that function:

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

Thank you both so much! That really helped!

 
0
 

Remember that raw_input() does return a string.

Question Answered as of 1 Year Ago by Gribouillis, vegaseat and Lucaci Andrew
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: