lundi 30 mars 2015

python if-statement with IndentationError

I have wrote a function to get commands in my database and now I am trying to write a function with more specify detail to enter to my database.But there is an error when I add a elif statement


Before:(Success)(ps :user is going to input function of the command first, if user input "switch" in the device buttom , he will get commands from my switch table, else he will get commands from router"):



def readciscodevice(function, device):
conn = sqlite3.connect('server.db')
cur = conn.cursor()
if device == "switch":
cur.execute(
"SELECT DISTINCT command FROM switch WHERE function =? or function='configure terminal' or function='enable' ORDER BY key ASC",
(function,))
read = cur.fetchall()
return read
elif device == "router":
cur.execute(
"SELECT DISTINCT command FROM router WHERE function =? or function='configure terminal' or function='enable' ORDER BY key ASC",
(function,))
read = cur.fetchall()
return read;
a = input("function:")
b = input("device:")
for result in readciscodevice(a,b):
print(result[0])


Then, I am trying to create a table called "showcommand" as show commands do not need to press "configure terminal" so I will make one more if statement but it seems not working...


After I add a if statement:



def readciscodevice(function, device):
conn = sqlite3.connect('server.db')
cur = conn.cursor()
if device == "switch":
cur.execute(
"SELECT DISTINCT command FROM switch WHERE function =? or function='configure terminal' or function='enable' ORDER BY key ASC",
(function,))
read = cur.fetchall()
return read
elif device == "router":
cur.execute(
"SELECT DISTINCT command FROM router WHERE function =? or function='configure terminal' or function='enable' ORDER BY key ASC",
(function,))
read = cur.fetchall()
return read;
elif device == "showcommand":
cur.execute(
"SELECT DISTINCT command FROM showcommand WHERE function =? or function='enable' ORDER BY key ASC",
(function,))
read = cur.fetchall()
return read;


a = input("function:")
b = input("device:")
for result in readciscodevice(a,b):
print(result[0])


The main error(which is int my last elif-statement[showcommand]) :



read = cur.fetchall()
IndentationError: unindent does not match any outer indentation level


and return read;



Trailing semicolon in the statement less... (Ctrl+F1)
This inspection detects trailing semicolons in statements.


I dun know what is going on? It looks no any problem in my mind but the problem is real. Thank you for helping!


Aucun commentaire:

Enregistrer un commentaire