Elif syntax error in Python

2024/11/18 3:39:37

This is my code for a if/elif/else conditional for a text-based adventure game I'm working on in Python. The goal of this section is to give the player options on what to do, but it says there is something wrong with the syntax of all of my "elif" statements.

if command.lower() in ["north", "n"]:#With invitationif "invitation" in items:castle()#Without 'Invitation'   else:print "As you walk towards the gate, a guard approaches you and pushes you away. \"Sorry, pal,\" says the guard,\"You can't get in the castle without a written invitation from the king\""town_center()elif command.lower() in ["east", "e"]:#market()elif command.lower() in ["south", "s"]:#forest()elif command.lower() in ["west", "w"]:#village()elif command.lower() in ["inventory", "i"]:print itemstown_center()elif command.lower() in ["look", "l"]:print "\nYou are standing in the center of the rural town of Grifiinhorn, a quaint rural town in the province of Bedrin. To the north, you see a giant castle sitting on top of a hill. To the east is the marketplace, and to the west is the village. To the south is exit gates of the town which faces the Dark Forest.\n" town_center()else:town_center()

I always get this error:

elif command.lower() in ["east", "e"]:^
SyntaxError: invalid syntax
Answer

Edit: although this is something that's also wrong, it's not the cause of his SyntaxError

There must be something in a code block. Because of your commenting out some lines:

elif command.lower() in ["south", "s"]:#forest()elif command.lower() in ["west", "w"]:

There is no code in the block after the first elif:, so you get a syntax error when the next elif: is found instead.

Add a pass statement in the blocks you have commented out:

elif command.lower() in ["south", "s"]:#forest()pass
elif command.lower() in ["west", "w"]:

pass does nothing, but it can form a block in cases like this.

https://en.xdnf.cn/q/118726.html

Related Q&A

Convert date from dd-mm-yy to dd-mm-yyyy using python [duplicate]

This question already has answers here:How to parse string dates with 2-digit year?(6 answers)Closed 7 years ago.I have a date input date_dob which is 20-Apr-53 I tried converting this to format yyyy…

sklearn pipeline transform ValueError that Expected Value is not equal to Trained Value

Can you please help me to with the following function where I got the error of ValueError: Column ordering must be equal for fit and for transform when using the remainder keyword(The function is calle…

How to show Chinese characters in Matplotlib graphs?

I want to make a graph based on a data frame that has a column with Chinese characters. But the characters wont show on the graph, and I received this error. C:\Users\march\anaconda3\lib\site-packages\…

nginx flask aws 502 Bad Gateway

My server is running great yesterday but now it returned a 502 error, how could this happen?In my access.log shows:[24/Aug/2016:07:40:29 +0000] "GET /ad/image/414 HTTP/1.1" 502 583 "-&q…

Let discord bot interact with other bots

I have a Python script for a Discord bot and I want it to send a message to another Bot and select the prompt option and then type in a message but I cant get the interaction done. It just sends the me…

Image has 3 channels but its in a grayscale color. If I change it to 1 channel, it goes into RGB

I started doing some image-processing in python and Ive stumbled upon an issue which is kind of confusing from a beginners perspective. I have a dataset of 1131 np arrays (images) of MRI on knee. The s…

Creating a Barplot using pyqt

I need plotting an animated bar chart with pyqtgraph. With animate i mean a chart, which updates his values given by a serial port. For now, a not-animated plot will be enough. I would like to implemen…

Stop Button in Tkinter

Im trying to have a turtle animation start with a button and stop with a button. Its very easy to start with a button but I cant seem to be able to figure out a stop button? Heres my code so far: imp…

binascii.Error: Incorrect padding How to decode the end with /

I received a string encoded with base64, I am using python to decode it, but decoding failed, I found that the string is followed by / ends, I dont know how to decode it, I havent found the answer, who…

How to match words in a list with user input in python?

I am working on program which takes user input and replaces the words in a list with x. eg is the word is sucks and user input is "this word is sucks". the output should be "this word i…