Reputation: 434
Very new to Python and I can't get to understand why the inputs is passing through as TRUE with second condition. I was expecting to print "," instead I'm getting ".":
def define_punctuation(inputs):
text = inputs.split()
if text[len(text) - 1] != '/end' and text[0] == 'how' or text[0] == 'when' or text[0] == 'what' or text[0] == 'why':
text = '?'
print(text)
elif text[0] != 'how' or text[0] != 'when' or text[0] != 'what' or text[0] != 'why' and text[len(text) - 1] == '/end':
text = '.'
print(text)
else:
text = ','
print(text)
define_punctuation('test test test')
Upvotes: 0
Views: 53
Reputation: 983
You must use like this
def define_punctuation(inputs):
text = inputs.split()
if (text[len(text) - 1] != '/end') and (text[0] == 'how' or text[0] == 'when' or text[0] == 'what' or text[0] == 'why'):
text = '?'
print(text)
elif (text[0] != 'how' or text[0] != 'when' or text[0] != 'what' or text[0] != 'why') and (text[len(text) - 1] == '/end'):
text = '.'
print(text)
else:
text = ','
print(text)
As in your case you have used like this
if text[0] != 'how' or text[0] != 'when' or text[0] != 'what' or text[0] != 'why' and text[len(text) - 1] == '/end':
Which say if any of condition goes write it will execute the statement
e.g
if False or False or True or False and False
Here this is wrong approach to write condition as it will execute when it will get True
Right approach is
if (False or True or False) and (False)
Upvotes: 2
Reputation: 375
I suppose that you want to this
(text[0] != 'how' or text[0] != 'when' or text[0] != 'what' or text[0] != 'why') and text[len(text) - 1] == '/end'
in the second (elif) statement.
Upvotes: 0