2

I'm new so sorry if my question isn't really specific or if the title is misleading, I've been trying to make a game in Python 2.7. So far everything is going great but there's one problem, I get a syntax error and I don't know how to fix it. The error says: "There's an error in your program: * can't assign to literal (Text game.py, line 28)" In this line I'm trying to assign 'n' a value of 1, here's the code:

print """
---------------------------------------------
|                                           |
|     TEXT GAME: THE BEGINNING!             |
|                                           |
---------------------------------------------
"""
print "What is your name adventurer?"
adv_name = raw_input("Please enter a name: ")
print "Welcome " + adv_name + ", I am called Colosso. I am the great hero of the town Isern. \nWhile I was protecting the surrounding forests I was attacked and killed. \nNow I am nothing but a mere spirit stuck in the realm of Life. \nI am here because I swore to slay the corrupt great king Blupri. \nBlupri still lives therefore I cannot travel to the realm of Death. \nI need you to slay him and his minions so I may rest in peace"
print """Do you want to help Colosso?:
1.) Yes (Start playing)
2.) No (Quit)
"""
dside = input("Please enter a number to decide: ")
if dside == 2:
    print "I knew you were a coward..."
    raw_input('Press Enter to exit')
elif dside == 1:
    print "Great! Let's get this adventure started"
    raw_input('Press Enter to continue')
print """This is the tutorial level, here is where you will learn how to play.
To move the letter of a direction, n is north, e is east, s is south, w is west.
Press the '<' key to move up and the '>' key to move down.
Try it!
"""
move = raw_input('Where would you like to go?: ')
"n" = 1
"e" = 2
"s" = 3
"w" = 4
"<" = 5
">" = 6
if move == 1:
    print "You move north."
if move == 2:
    print "You move east."
if move == 3:
    print "You move south."
if move == 4:
    print "You move west."
print move

I've tried it with out the quotation marks and single quotation marks but neither work Any help or advice is appreciated.

1
  • The error means that "literal" = value is being done. This is not allowed and make no more sense than 42 = 0. 42 is not 42 and not 0. Likewise, "literal" is "literal" and not any other value. Commented Jun 2, 2012 at 20:31

2 Answers 2

2

Rob is right, and also the following lines of the code don't make much sense.

I'd suggest just that:

move = raw_input('Where would you like to go?: ')

if move == 'n':
    print "You move north."
elif move == 'e':
    print "You move east."
elif move == 's':
    print "You move south."
elif move == 'w':
    print "You move west."

Or if you really want to map the input to numbers for some reason, consider creating a dictionary:

directions = {"n": 1, "e": 2, "s": 3, "w": 4, "<": 5, ">": 6}

Then you can do:

if directions[move] == 1:
     # etc
Sign up to request clarification or add additional context in comments.

3 Comments

It is quicker if after the first if ... you use else if, so it only checks that condition if all previous ones have failed.
@C0deH4cker very true, I didn't notice that while copying & pasting. Updating the answer now.
Oh duh, elif. I've been switching between langs all day (c, cpp, objc, php, java), and only python uses elif. I should have remembered this, though, as python is my best language.
1

The "n" is interpreted by Python as a string literal which means that it itself is a value and you cannot assign a value to another value. The token on the left hand side of the = needs to be a variable.

I suggest removing the double quotes around the n in line 28. Not a python coder but that's what I would do instinctively.

3 Comments

Thanks Rob! That works, I must have read the error wrong when I removed them first time. It was really frustrating, thanks for the help. I appreciate it
It's a pleasure! It's fun answering questions on languages that are new to me. I now hand you over to Lev who appears to have more Python experience than I ;-) I have installed Python to play with your code though. So thank you for asking the question.
@user1432812 Consider accepting the (most) helpful answer by checking the tick to the left of it. This way you indicate that the question is resolved, and also reward the person who posted the answer. There's no rush though, you can do it later.

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.