1,105,263 Community Members

Syntax Error??

Member Avatar
pars99
Junior Poster in Training
57 posts since Nov 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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.

Member Avatar
Gribouillis
Posting Maven
3,452 posts since Jul 2008
Reputation Points: 1,140 [?]
Q&As Helped to Solve: 883 [?]
Skill Endorsements: 18 [?]
Moderator
 
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.

Member Avatar
pars99
Junior Poster in Training
57 posts since Nov 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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?

Member Avatar
Gribouillis
Posting Maven
3,452 posts since Jul 2008
Reputation Points: 1,140 [?]
Q&As Helped to Solve: 883 [?]
Skill Endorsements: 18 [?]
Moderator
 
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.

Member Avatar
Lucaci Andrew
Master Poster
746 posts since Jan 2012
Reputation Points: 128 [?]
Q&As Helped to Solve: 116 [?]
Skill Endorsements: 18 [?]
 
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
Member Avatar
pars99
Junior Poster in Training
57 posts since Nov 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Thank you both so much! That really helped!

Member Avatar
vegaseat
DaniWeb's Hypocrite
6,984 posts since Oct 2004
Reputation Points: 1,544 [?]
Q&As Helped to Solve: 1,872 [?]
Skill Endorsements: 67 [?]
Moderator
 
0
 

Remember that raw_input() does return a string.

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