0

I am brand new to coding in python (and mostly in general, apart for some basic JS knowledge). I am starting off by writing a fun text-based interactive story game, which runs through command prompt. The basic premise is that the player is interacting with their computer's built-in ai assistant. Everything seemed to be working out fine, until I realized that some of the possible responses were not being recognized. I cannot figure out where the problem lies. My description won't do the problem any justice, so I'll just show the code itself. (I am using visual studio for this)

    print("Assistant: Good Morning, User.")
response=input("Enter Response: ").lower()
print("Processing, please wait...")
import time
time.sleep(3)
if("good morning" in response):
    print("Assistant: Yes, I suppose it is.")
elif("hello" or "hi" in response):
    print("Assistant: I hope you are well today.")
elif("no" or "eh" in response):
    print("Assistant: I am sorry to hear that, user.")
else:
    print("Assistant: ...")
time.sleep(1)
print("Assistant: Would you like me to display the news, as usual?")
res1=input("Enter Response: ").lower()
print("Processing, please wait...")
time.sleep(3)
if("yes" or "sure" in res1):
    print("Assistant: Okay, here is today's news.")
elif("no" or "i'm good" in res1):
    print("Assistant: Is that so? As you wish, user.")
  • 1
    Statements such as `elif("hello" or "hi" in response)` will *always* evaluate to `True` as the implementation of the `or` operator might be as you are expecting. The actual evaluation is: `("hello") or ("hi" in response)`. The `or` operator must have *two* operands. Revise this and you’ll see an improvement in the results of your program. [See here for additional reference](https://stackoverflow.com/a/20002504/6340496). – S3DEV Apr 29 '22 at 14:30
  • as @S3DEV suggested your syntax is wrong. Instead you should do `if response in ("hello", "hi"):` –  Apr 29 '22 at 14:32
  • Does this answer your question? [How to test multiple variables for equality against a single value?](https://stackoverflow.com/questions/15112125/how-to-test-multiple-variables-for-equality-against-a-single-value) – Yevhen Kuzmovych Apr 29 '22 at 14:32
  • Does this answer your question? [Why does "a == x or y or z" always evaluate to True?](https://stackoverflow.com/questions/20002503/why-does-a-x-or-y-or-z-always-evaluate-to-true) – S3DEV Apr 29 '22 at 14:33
  • @SembeiNorimaki - Possibly, but not necessarily. The `str in iter` syntax tests if the *full* string is listed in the iterable. Whereas the `str in str or str in str` syntax can test substrings. – S3DEV Apr 29 '22 at 14:36
  • I think from the code the user wants to check if the response introduced is either "hello" or "hi" to give a particular response. –  Apr 29 '22 at 14:37
  • @SembeiNorimaki yep, that's right. I don't want to have any mixups where it finds "hi" in a bigger response that might be something entirely different lol. Thanks for the help. – FeelingDangerouslyUmami Apr 29 '22 at 14:46

0 Answers0