0

Apparently, my string recognition algorithm is not working properly. It is returning the wrong responses based on the user's input. I know it's probably something simple, but I just can't see what it is right now. I done the research and I haven't found another python issue here where when the user input is entered, the wrong response is returned. Are my if statements properly formed? Is there an issue with the string search?

import random

def po_response():
response = {1 : 'This is random response A from po_response()',
            2 : 'This is random response B from po_response()',
            3 : 'This is randomw response C from po_response()'}
answer = random.sample(response.items(),1)
print(answer)
main()


def greetings_response():
response = {1 : 'This is response A from greetings_response()',
            2 : 'This is response B from greetings_response()',
            3 : 'This is response C from greetings_response()'}
answer = random.sample(response.items(),1)
print(answer)
return response
main()


def main():
userRequest = input('Welcome, Please enter your request. \n')
userReq = userRequest.lower()
if 'how are you' or 'how\'s it going' in userReq:
    print('first if')
    print('The value of the input is ')
    print(userReq)
    greetings_response()


elif 'ship' + 'po' or 'purchase order' in userReq:
    print('elif')
    print('The value of the input is ')
    print(userReq)
    po_response()


else:
    print('Please re-enter your request')

main() 

Here is the response I get when I enter 'ship po'

   Welcome, Please enter your request. 
   >>>ship po
   first if
   The value of the input is 
   ship po
   [(2, 'This is response B from greetings_response()')] 

It should not go to the greetings_response() function, it should go to the po_response() function. Not sure why it's acting this way.

brohjoe
  • 854
  • 4
  • 17
  • 39
  • 2
    Does this post display exactly your actual code indentation? – Jongware Aug 08 '16 at 21:23
  • Possible duplicate of [How do I test one variable against multiple values?](http://stackoverflow.com/questions/15112125/how-do-i-test-one-variable-against-multiple-values) – OneCricketeer Aug 08 '16 at 21:25
  • Specifically... `'how are you' or 'how\'s it going' in userReq` – OneCricketeer Aug 08 '16 at 21:26
  • @RadLexus...It is not. It's fitted to the Stackoverflow code window. The code is properly indented in the IDLE dev environment. – brohjoe Aug 08 '16 at 21:28
  • So edit the post and fix it! Also provide an input and output (current & desired) example – Ohad Eytan Aug 08 '16 at 21:33
  • Your question is definitely a duplicate... `if 'how are you'` is immediately evalutated to True, so `first if` gets printed. – OneCricketeer Aug 08 '16 at 22:00
  • Also, you would have to enter `shippo` for the second if statement anyways... As per `'ship' + 'po'` – OneCricketeer Aug 08 '16 at 22:00
  • @cricket_007...of course, you're right about the concatenation. At the time I didn't see that it would require a space in between in order to recognize "ship po". Thanks. – brohjoe Apr 23 '18 at 00:34

1 Answers1

1

Test using or seems wrong. Maybe you meant if ('how are you' in userReq) or ('how\'s it goind' in userReq) ? Have a look to Python operators precedence.

neodelphi
  • 2,706
  • 1
  • 15
  • 22
  • 1
    What about `if userReq in {'how are you', 'how\'s it going'}`? – OneCricketeer Aug 08 '16 at 21:58
  • @cricket_007 Depends on what you want to achieve. `if userReq in {'how are you', 'how\'s it going'}` won't match 'how are you bob ?'. – neodelphi Aug 08 '16 at 22:18
  • 1
    Fair point. I assumed the question was doing exact string matching, not substrings of the input – OneCricketeer Aug 08 '16 at 22:19
  • 1
    @cricket_007 Then your proposal is correct and much better than mine. – neodelphi Aug 08 '16 at 22:21
  • @neodelphi: So how would you do it if you didn't want exact matching, but only keyphase matching. I think we're in NLTK territory now. Say you wanted to match, 'how are you bob?' as well as 'how are you?', with the keyphase being 'how are you'. – brohjoe Aug 09 '16 at 21:18