Home > Blockchain >  how can I do the action based on user input in python
how can I do the action based on user input in python

Time:10-01

I have this code

phrase = input("Enter your message: ")

if 'hello' in phrase:
  print('Hello, Human')
if 'what is your favorite color' in phrase:
  print("It's blue")
else:
  print('unavailable')

how can it reply based on user like if he say hello and then what is your favorite color it will reply hello then it's blue like below:

Enter your message: hello, what is your favorite color
Hello, Human
It's blue

and that's okay but what if I want to automatically change like if he say what is your favorite color and then hello it will reply it's blue and then hello so the output should like this:

Enter your message: what is your favorite color, hello
It's blue
Hello, Human

but not like this:

Enter your message: what is your favorite color, hello
Hello, Human
It's blue

CodePudding user response:

Your question is a bit too unspecific.. please specify it a bit more.

For the favorite color, since the user might ask something like "what's your favorite color", your program won't respond with blue because it's different from "what is your favorite color", so just have the program check for "favorite color".

phrase = input("Enter your message: ")

if 'hello' in phrase:
  print('Hello, Human')
elif 'favorite color' in phrase:
  print("It's blue")
else:
  print('unavailable')

CodePudding user response:

phrase = input("Enter your message: ")

if 'hello' in phrase:
  print('Hello, Human')
  phrase1 = input('Enter your message: ')
  if phrase1 == 'what is your favorite color':
    print('its blue')
elif 'what is your favorite color' in phrase:
  print("It's blue")
  phrase2 = input('Enter your message: ')
  if phrase2 == 'hello':
    print('hello human')
else:
  print('unavailable')
  • Related