-1

I created this jokey therapy bot (not serious) that in a session will learn new words if they're not already in the two lists. In the def learning_response there is an else statement:

else:
        print('Your emotions are too complex for me @{}"£$"${%£$')

But it doesn't actually get expressed. Does anyone have an idea why?

Any advice would be much appreciated!

EDIT: People have explained that the problem is with the 'or' part of the function. I can appreciate that I need to change it but don't completely understand why. Any ideas?

===========================================================================

happy_response = ['good', 'great', 'brilliant','happy','excited','amazing']
unhappy_response = ['not good','bad','shit','unhappy','crap','low']

running = True
while running==True:

    how_you = input('How are you today? ')
    if how_you == 'Quit':
        break
    #how_scale = input('On a scale of 1-10..1 being very low, 10 being great..how are you feeling? ')

    def positive_response():
        print('Good. Thatll be £50 please')

    def supportive_response():
        print('Oh well....ever thought going skipping? That always cheers sad bot up.')

    def learning_response():
        happy_or = input('I dont understand. Is this a happy or unhappy response? ')
        if happy_or == 'happy' or 'Happy':
            happy_response.append(how_you)
            print('Thank you. Love bot has learned a new positive word and become even more powerful.') 
        elif happy_or == 'unhappy' or 'Unhappy':
            unhappy_response.append(how_you)
            print('Thank you. Love bot has learned a new negative word and become even more powerful.')
        else:
            print('Your emotions are too complex for me @{}"£$"${%£$')

    def assess_mood():
        if how_you in happy_response:
            positive_response()
        elif how_you in unhappy_response:
            supportive_response()
        else:
            learning_response()

    assess_mood()

print('Ended...that will be £50 please')
John H
  • 47
  • 1
  • 7
  • 6
    This is a classic one. Python doesn't understand x == y or z. You'd need x == y or x == z. – cs95 Jul 18 '17 at 11:39
  • 2
    Better would be `if happy_or.lower()=='happy':...` – khelwood Jul 18 '17 at 11:39
  • 2
    But you could do `x in (y, z)`, in this case `happy_or in ('happy', 'Happy')` – Ma0 Jul 18 '17 at 11:40
  • 2
    @AndyG It does not seem to be a duplicate of 'Use of “global” keyword in Python'. – khelwood Jul 18 '17 at 11:42
  • Nice thank you!! It's all fixed now and I've learned something new :) To be honest I don't understand exactly why it's fixed..I can appreciate the need to do it but don't understand how that would stop any of the if/elif/else from being expressed. – John H Jul 18 '17 at 12:04
  • @JohnH Since you're saying that you don't understand yet what the problem was: The `or` does not work the way you think it does. You cannot use `or` to give multiple options to a `==` comparison. Instead, or combines boolean values, and any value is a boolean value in Python. Specifically, the expression `happy_or == 'happy' or 'Happy'` will always result in a boolean true value, because it combines the boolean values of the two expressions `happy_or == 'happy'` and `'Happy'`, and `'Happy'` is a boolean true value, so the whole expression is always true. – Rörd Jul 18 '17 at 13:37

1 Answers1

0
def learning_response():
    happy_or = input('I dont understand. Is this a happy or unhappy response? ')
    if happy_or == 'happy' or happy_or == 'Happy':
        happy_response.append(how_you)
        print('Thank you. Love bot has learned a new positive word and become even more powerful.') 
    elif happy_or == 'unhappy' or happy_or == 'Unhappy':
        unhappy_response.append(how_you)
        print('Thank you. Love bot has learned a new negative word and become even more powerful.')
    else:
        print('Your emotions are too complex for me @{}"£$"${%£$')

Fix your ifs. Every expression in the if statement must be complete, python doesn't understand lexical context, so after an or you need to provide the full statement.

K. Kirsz
  • 1,384
  • 10
  • 11