Python If Statement Defaults to an elif
Posted
by
Brad Carvalho
on Stack Overflow
See other posts from Stack Overflow
or by Brad Carvalho
Published on 2012-09-16T03:09:18Z
Indexed on
2012/09/16
3:37 UTC
Read the original article
Hit count: 223
python
Not sure why my code is defaulting to this elif. But it's never getting to the else statement. Even going as far as throwing index out of bound errors in the last elif. Please disregard my non use of regex. It wasn't allowed for this homework assignment. The problem is the last elif before the else statement.
Cheers, Brad
if item == '':
print ("%s\n" % item).rstrip('\n')
elif item.startswith('MOVE') and not item.startswith('MOVEI'):
print 'Found MOVE'
elif item.startswith('MOVEI'):
print 'Found MOVEI'
elif item.startswith('BGT'):
print 'Found BGT'
elif item.startswith('ADD'):
print 'Found ADD'
elif item.startswith('INC'):
print 'Found INC'
elif item.startswith('SUB'):
print 'Found SUB'
elif item.startswith('DEC'):
print 'Found DEC'
elif item.startswith('MUL'):
print 'Found MUL'
elif item.startswith('DIV'):
print 'Found DIV'
elif item.startswith('BEQ'):
print 'Found BEQ'
elif item.startswith('BLT'):
print 'Found BLT'
elif item.startswith('BR'):
print 'Found BR'
elif item.startswith('END'):
print 'Found END'
elif item.find(':') and item[(item.find(':') -1)].isalpha():
print 'Mya have found a label'
else:
print 'Not sure what I found'
© Stack Overflow or respective owner