Is it possible to put various conditions in the form of a list in the if conditional statement?

Asked 2 years ago, Updated 2 years ago, 84 views

def chat():
    print('start chatbot')
    while True:
        inp = input("user :")

        ifinf == "End":
            print ('Exit chatbot service')
            break

        data = "Hello" or "Hi"
        if data:

            print("How may I help you?")
        else:
            print('Will you say that again?')

chat()

When the input value is 'Hello', 'Hello', 'Hi', and 'Hi', "How can I help you?""
When another value comes in, "Can you say that again?"

I want to do it, but I don't know

If you do that, you'll get "What can I do for you" in any input <
Should I use the for statement? Where is the difference?

python if문 conditional-statement for

2022-09-22 18:05

2 Answers

data = ["Hello", "Hello", "Hi"]
if inp in data:
    How may I help you?')


2022-09-22 18:05

The code you wrote is always 'How can I help you?The reason why it only appears is because the data value in the code below is true which is boolean.

def chat():
    print('start chatbot')
    while True:
        inp = input("user :")

        ifinf == "End":
            print ('Exit chatbot service')
            break

        data = "Hello" or "Hi" or "High" # where data is set to true
        if data: 
            # Data is true, so always run this place
            print("How may I help you?")
        else:
            print('Will you say that again?')

chat()

To make it work the way you want it to,

def chat():
    print('start chatbot')
    while True:
        inp = input("user :")

        ifinf == "End":
            print ('Exit chatbot service')
            break

        data = ["Hello", "Hello", "Hi"]
        if inp in data:
            # Perform if the entered character is in the data list
            print("How may I help you?")
        else:
            # If you don't, follow this place
            print('Will you say that again?')

chat()


2022-09-22 18:05

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.