0

I'm just an amateur at python here but I was trying to code a short program that can answer questions based on inputs that match strings in a list. Here is my code below:

helloInputs = ["hello", "hi", "hey there", "yo", "hello", ]
howAreInputs = ["how are you", "how are you doing", "how you doing", "how are ya", "sup"]
ageInputs = ["how old are you", "what is your age", "age", "when were you born"]



a = raw_input("Type something here: ")

if a.upper() or a.lower() in helloInputs:
    print("Hello there!")

if a.upper() or a.lower() in ageInputs:
    print("My name is PySpeechBot. I was made by developer Nicholas A. on June 6, 2020")

if a.upper() or a.lower() in howAreInputs:
    print("Im feeling good today! This is actually the only response I am programmed to say back...") 

Anyways, the problem I am having is that when I input something simple like 'Hello', it will respond back with all possible answers instead of just one. Again, not the best programmer here, but if this could be fixed, i would appreciate it.

bereal
  • 32,519
  • 6
  • 58
  • 104
  • 3
    Get rid of the `a.upper() or` in each test. It's forcing them all to be `True` and isn't needed since you're comparing against all-lowercase strings. Just use `a.lower() in string_list`. – Tom Karzes Jun 06 '20 at 16:39
  • 3
    Beside the point, but if you're just starting to learn Python, learn Python 3 instead of Python 2. Python 2 hit end-of-life in January, and Python 3 is much better anyway. – wjandrea Jun 06 '20 at 16:40

1 Answers1

0

If you wanted to check if multiple items were in a list, you could use a solution from How to check if one of the following items is in a list? For example:

if any(x in helloInputs for x in [a.upper(), a.lower()]):
    print("Hello there!")

But that's not really what you're trying to accomplish. Where your input lists are all lowercase, you should just remove a.upper().

As well, use elif for if-statements that have multiple possibilities, and use a variable instead of writing a.lower() over and over:

a = raw_input("Type something here: ")
b = a.lower()

if b in helloInputs:
    print("Hello there!")
elif b in ageInputs:
    print("My name is PySpeechBot. I was made by developer Nicholas A. on June 6, 2020")
elif b in howAreInputs:
    print("Im feeling good today! This is actually the only response I am programmed to say back...") 
wjandrea
  • 28,235
  • 9
  • 60
  • 81