简体   繁体   English

如何根据 python 中的用户输入执行操作

[英]how can I do the action based on user input in python

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:没关系,但是如果我想自动更改,就像他说你最喜欢的颜色是什么然后你好它会回复它是蓝色然后你好所以 output 应该是这样的:

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

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')
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')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Python 3中基于用户输入调用函数? - How do I call a function based on user input in Python 3? Python 中的 Input() function:如何根据用户输入创建操作? - Input() function in Python: How can I create an action out of user input? 如何在 Rasa 的自定义操作中获取用户输入? - How do I get user input in a custom action in Rasa? 如何根据Python中的用户输入在电子表格中返回单元格值? - how do I return cell values in a spreadsheet based on user input in Python? 如何根据用户在 python 中的输入打印列表中的某些项目? - How do i print certain items in a list based on user input in python? 如何根据 Python 中的用户输入设置变量的值? - How do you set the value of a variable based on user input in Python? 如何根据用户输入创建变量? (Python) - How do you create variables based on user input? (Python) 我如何创建一个函数,该函数根据在Python中输入相同输入的次数,使用户输入具有不同的结果? - How do I create a function that allows for user input to have a different outcomes based on how many times the same input was entered in Python? 如何让用户输入可在python中使用的方程式? - How do I get user to input an equation that I can use in python? 如何根据用户输入在 SymPy 中创建符号? - How do I create Symbols in SymPy based on user input?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM