I'm using python IDLE v. 2.7.2 and I get the same error report with my program.

>>> def menu(list, question):
    for entry in list:
        print 1 + list.index(entry),
        print ") " + entry

    return input(question) - 1

[B]items[/B] = ["Cup","Vase","Table","Lamp","Bowl","Door"]

keylocation = 3

keyfound = 0

loop = 1
print "Welcome to The Great Text Adventure!"
print "One day you wake up, in a room, locked in by yourself."
print "Could there be a key?
print "In the room you see."
print len (items), "things:"
for x in items:
    print x
print ""
while loop == 1
    choice = menu(items, "What do you want to inspect? ")
    if choice == 0:
        if choice == keylocation:
            print "You found a key in the cup."
            
            print ""
            keyfound = 1
        else:
            print "You found nothing in the cup."
            print ""
    elif choice == 1:
        if choice == key location
            print "You found a key in the vase."
            print ""
            
            keyfound = 1
        else:
            print "You found noting in the vase."
            
            print""
    elif choice == 2:
        if choice == keylocation:
            print "You found a key on the table."
            print""
            keyfound = 1
        else:
            print "You found nothing on the table."
            print ""
            
    elif choice == 3:
        if choice ==keylocation:
            print "You found a key under the lamp."
            print""
            keyfound = 1
        else:
            print "You found nothing under the lamp."
            print ""
    elif choice == 4:
        if choice == keylocation:
            print "You found a key in the bowl."
            print ""
        else:
            print "You found nothing in the bowl."
            print ""
            
    elif choice == 5:
        if keyfound == 1:
            loop = 0
            print "You put in the key, turn it, and hear a click."
            
            print ""
        else:
            print "The door is locked, you need to find a key."
            print ""

unlockeddoor = 0
loop2 = 1
doors = ["Blue Door","Green Door","Red Door"]
print "You walk into the next room."
print "You see a blue door to your left, a green door to your right, and a red door straight ahead."
while loop2 == 1
    choice = menu(doors,"Which door should you take? ")
    if choice == 0
        if choice == unlockeddoor
            print "The door is unlocked."
            print ""
            loop2 = 0
        else:
            print "The door was locked."
            print ""
            
    elif choice == 1
        if choice == unlockeddoor
            print "The door is unlocked."
            print ""
            loop2 = 0
        else:
            print "The door was locked."
            print ""
            
    elif choice == 2
        if choice == unlockeddoor
            print "The door is unlocked."
            print ""
            loop2 = 0
        else:
            print "The door was locked."
            print ""

correctdirection = 1
directions = ["North","South","East","West"]
loop3 = 1
print "You walked through the door and saw sunlight."
print "You realized that the town is abandoned."
print "You can go North, South, East, or West."
while loop3 == 1
    choice = menu(directions, "Which direction should you go? ")
    if choice == 0
        if choice == correctdirection
            print "You walked 2 miles North and found a bridge over a canyon."
            print ""
            loop3 = 0
        else:
            print "You walked 2 miles North and found a canyon. You had to turn around."
            print ""
            
    elif choice == 1
        if choice == correctdirection
            print "You walked 2 miles South and found a bridge over a river."
            print ""
            loop3 = 0
        else:
            print "You walked 2 miles South and found a river. You had to turn around."
            print ""
            
    elif choice == 2
        if choice == correctdirection
            print "You walked 2 miles East and found a tunnel through a mountain."
            print ""
            loop3 = 0
        else:
            print "You walked 2 miles East and found a mountain. You had to turn around."
            print ""
            
    elif choice == 3
        if choice == correctdirection
            print "You walked 2 miles West and found a tunnel through a mountain."
            print ""
            loop3 == 0
        else:
            print "You walked 2 miles West and found a river. You had to turn around."
            print ""
print "You won!"
print "Thank you for playing The Great Text Adventure."
print "Check back later for more updates."
print "Credits: http://www.********.com/ for the idea and basic script."
print "Scripted mostly by ****** *******."
SyntaxError: invalid syntax
>>>

(The *'s at the end are to hide my name and a website i used) Please help me with this, its my first program.(bold was highlited)

Recommended Answers

All 11 Replies

You must learn to read the traceback, which is

File "./jeuidiot.py", line 18
    print "Could there be a key?
                               ^
SyntaxError: EOL while scanning string literal

It means that python encountered an End Of Line error at line 18 while the parser was scanning a literal string. I think it's pretty explicit, and you can easily conclude that you must add a double quote at the end of print "Could there be a key? .

Don't copy and paste such a big code to the idle shell. Open a new file with idle and write the code in this file, then press the F5 key.

You can condense all of the print statements if you want.

print """Welcome to The Great Text Adventure!
One day you wake up, in a room, locked in by yourself.

Could there be a key?
In the room you see."""

You can also use a list to eliminate redundant code.

## the same for keylocation, unlockeddoor, etc.
    choice_list = ["North and found a bridge over a canyon.",
                   "South and found a bridge over a river.",
                   "East and found a tunnel through a mountain.",
                   "West and found a tunnel through a mountain."]
    incorrect_list=["North and found a canyon. You had to turn around.",
                    "South and found a river. You had to turn around.",
                    "East and found a mountain. You had to turn around.",
                    "West and found a river. You had to turn around."]

    if 0 <= choice <= 3:
        print "You walked 2 miles",
        if choice == correctdirection:
            print choice_list[choice]
            loop3 = 0
        else:
            print incorrect_list[choice]
        print ""

You must learn to read the traceback, which is

File "./jeuidiot.py", line 18
    print "Could there be a key?
                               ^
SyntaxError: EOL while scanning string literal

It means that python encountered an End Of Line error at line 18 while the parser was scanning a literal string. I think it's pretty explicit, and you can easily conclude that you must add a double quote at the end of print "Could there be a key? .

Don't copy and paste such a big code to the idle shell. Open a new file with idle and write the code in this file, then press the F5 key.

Thanks for pointing that out. I still get that same error though.:(
items is still highlighted.
I'm still learning python and know absolutely nothing about debugging. I bet you can help.

Thanks for pointing that out. I still get that same error though.:(
items is still highlighted.
I'm still learning python and know absolutely nothing about debugging. I bet you can help.

Did you save the code to a python file (.py) and run the file with idle ? Then what is the complete traceback ? What you can do is zip the file and attach the zipped version to a post.

Did you save the code to a python file (.py) and run the file with idle ? Then what is the complete traceback ? What you can do is zip the file and attach the zipped version to a post.

Ok thanks. I got a different error message this time. I worked on it a lot last night. My new error is "Unsupported characters in input" Also "/Users/thefxperson/Documents/TextAdventure.py".Could you please tell me if my restart game portion works. Thanks. My zip wouldn't work so heres the code.

def menu(list, question):
    for entry in list:
        print 1 + list.index(entry),
        print ") " + entry

    return input(question) - 1

items = ["Cup","Vase","Table","Lamp","Bowl","Door"]

keylocation = 3

keyfound = 0

loop = 1
print "Welcome to The Great Text Adventure!"
print "One day you wake up, in a room, locked in by yourself."
print "Could there be a key?"
print "In the room you see: A cup, a vase, a table, a lamp, a bowl, and a door."
while loop == 1
    choice = menu(items, "What do you want to inspect? (Enter a number between 0 and 5. e.g. 0 = bowl)")
    if choice == 0:
        if choice == keylocation:
            print "You found a key in the cup."
            
            print ""
            keyfound = 1
        else:
            print "You found nothing in the cup."
            print ""
    elif choice == 1:
        if choice == key location
            print "You found a key in the vase."
            print ""
            
            keyfound = 1
        else:
            print "You found noting in the vase."
            
            print""
    elif choice == 2:
        if choice == keylocation:
            print "You found a key on the table."
            print""
            keyfound = 1
        else:
            print "You found nothing on the table."
            print ""
            
    elif choice == 3:
        if choice ==keylocation:
            print "You found a key under the lamp."
            print""
            keyfound = 1
        else:
            print "You found nothing under the lamp."
            print ""
    elif choice == 4:
        if choice == keylocation:
            print "You found a key in the bowl."
            print ""
        else:
            print "You found nothing in the bowl."
            print ""
            
    elif choice == 5:
        if keyfound == 1:
            loop = 0
            print "You put in the key, turn it, and hear a click."
            
            print ""
        else:
            print "The door is locked, you need to find a key."
            print ""

unlockeddoor = 0
loop2 = 1
doors = ["Blue Door","Green Door","Red Door"]
print "You walk into the next room."
print "You see a blue door to your left, a green door to your right, and a red door straight ahead."
while loop2 == 1
    choice = menu(doors,"Which door should you take? ")
    if choice == 0
        if choice == unlockeddoor
            print "The door is unlocked."
            print ""
            loop2 = 0
        else:
            print "The door was locked."
            print ""
            
    elif choice == 1
        if choice == unlockeddoor
            print "The door is unlocked."
            print ""
            loop2 = 0
        else:
            print "The door was locked."
            print ""
            
    elif choice == 2
        if choice == unlockeddoor
            print "The door is unlocked."
            print ""
            loop2 = 0
        else:
            print "The door was locked."
            print ""

correctdirection = 1
directions = ["North","South","East","West"]
loop3 = 1
print "You walked through the door and saw sunlight."
print "You realized that the town is abandoned."
print "You can go North, South, East, or West."
while loop3 == 1
    choice = menu(directions, "Which direction should you go? ")
    if choice == 0
        if choice == correctdirection
            print "You walked 2 miles North and found a bridge over a canyon."
            print ""
            loop3 = 0
        else:
            print "You walked 2 miles North and found a canyon. You had to turn around."
            print ""
            
    elif choice == 1
        if choice == correctdirection
            print "You walked 2 miles South and found a bridge over a river."
            print ""
            loop3 = 0
        else:
            print "You walked 2 miles South and found a river. You had to turn around."
            print ""
            
    elif choice == 2
        if choice == correctdirection
            print "You walked 2 miles East and found a bridge over a canyon."
            print ""
            loop3 = 0
        else:
            print "You walked 2 miles East and found a canyon. You had to turn around."
            print ""
            
    elif choice == 3
        if choice == correctdirection
            print "You walked 2 miles West and found a bridge over a river."
            print ""
            loop3 == 0
        else:
            print "You walked 2 miles West and found a river. You had to turn around."
            print ""

print "You walked over the bridge and found a car"
print "Where will you go? (Note that from here on affects the outcome of the game)"
loop4 = 1
future1 = ["Mountain","Ocean","Jungle"]
print "You can drive to the: Mountains, Ocean, Jungle."
while loop4 == 1
    choice = menu(future1, "Where will you drive?")
    if choice == 0
        print "You drove to the mountains."
        print "You see 2 towns, a city, and a village."
        citorvil = ["City","Village"]
        citorvilloop = 1
        while citorvilloop == 1
            choice = menu(citorvil, "Will you drive to the city or the village?")
            if choice == 0
                print "You drove to the city."
                print "Will you be a chef, or an astronaut?"
                citjob = ["Chef","Astronaut"]
                citjobloop = 1
                while citjobloop == 1
                    choice = menu(citjob, "What job will you chose?")
                    if choice == 0
                        print "You chose to be a chef."
                        print "You lived a long and happy life"
                        print ""
                        loop4 = 0
                    else:
                        print "You chose to be an astronaut."
                        print "Your first mission to Mars was successful."
                        print "You never returned from your second…"
                        print ""
                        loop4 = 0
        else:
            print "You drove to the village"
            print "Will you be a farmer, or the mayor?"
            viljob = ["Farmer","Mayor"]
            viljobloop = 1
            while viljobloop == 1
                choice = menu(viljob, "What job will you chose?")
                if choice == 0
                    print "You chose to be a farmer."
                    print "You lived a long and happy life."
                    print ""
                    loop4 = 0
                else:
                    print "You chose to be the mayor."
                    print "You lived a long and happy life."
                    print ""
                    loop4 = 0
    elif choice == 1
        print "You drove to the ocean."
        print "You have 2 choices."
        print "You can found a town, or build a boat."
        toworboa = ["Town","Boat"]
        toworboaloop = 1
        while toworboaloop == 1
            choice = menu(toworboa, "Will you found a town or build a boat?")
            if choice == 0
                print "You founded a very successful town."
                print "You lived a long and happy life."
                print ""
                loop4 = 0
            else:
                print "You built a boat."
                print "You sailed into the ocean, never to be seen again...."
                print ""
                loop4 = 0
    elif choice == 2
    print "You drove to the jungle."
    print "You got lost in the jungle."
    print "You find a jungle tribe."
    print "You can join this tribe, or try to find your way out of the jungle."
    triorwan ["Tribe","Wander"]
    triorwanloop = 1
    while triorwanloop ==1
        choice = menu(triorwan, "Will you join the jungle tribe, or try to find your way out of the jungle?")
        if choice == 0
            print "You joined the jungle tribe."
            print "You became the tribe master."
            print "You lived a long and happy life."
            print ""
            loop4 = 0
        else:
            print "You tried to find your way out of the jungle."
            print "You failed."
            print "You were never seen again…."
            print ""
            loop4 = 0

print "You won!"
print "Thank you for playing The Great Text Adventure."
print "Check back later for more updates."
print "Credits: http://www.sthurlow.com/ for the idea and basic script."
print "Scripted mostly by ****** *******."
print "Debugging by various users on, http://www.daniweb.com/software-development/python/threads/410460/1751635#post1751635 "
restartgame = ["Restart"]
choice = menu(restartgame, "Hit 0 to restart.")
if choice == 0
    print "Restarting game"
    print ""
    loop = 1

The invalid character is the dots at line 182. Replace it with

print "You never returned from your second..."

Otherwise there are innumerable 'if' lines and 'while' lines with a missing colon : at the end. Python will complain.

The invalid character is the dots at line 182. Replace it with

print "You never returned from your second..."

Otherwise there are innumerable 'if' lines and 'while' lines with a missing colon : at the end. Python will complain.

Ok now I get a thing that says invalid syntax and highlights the 7 in

Python 2.7.2

before the program starts. I'm going to add you to the credits as a debugger. In reply to the "if" and "while" part; does this mean I put a ":" at the end of every "if" and "while" line?

Every indented block of logical lines is preceded by a colon at the end of the previous line.

Every indented block of logical lines is preceded by a colon at the end of the previous line.

Ok let me try that after school. I will get back to you in 6 hours.

EDIT:
I did what you said and I still get: "error invalid syntax" when I try to run it. It still highlights the 7 in

Python 2.7.2

You have nothing such in your code posted. Are you sure you are not trying to run log of your interactive session, instead of script file?

You have nothing such in your code posted. Are you sure you are not trying to run log of your interactive session, instead of script file?

I dont know what your saying. All I know is the basic language. What I do understand is that I open the file, save, then go under the run menu, then hit run module.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.