使用 Python 回复用户文本
你可以使用 if-elif-else 语句来解决这个问题。并且为了更贴合要求,它将询问有效选项,直到给定的选项出现在列表中,我们可以使用 while 循环。当选项有效时,就中断循环,否则,它将一再请求输入。
你应该将输入当作整数,为此你需要使用 int() 方法将输入强制转换位整数。
示例
请查看以下代码以遵循给定要点。
print("Come-on in. Need help with any bags?")
while True:
# loop is used to take option until it is not valid.
bag = int(input("(1)Yes (2)No Thanks (3)I'll get 'em later\nTYPE THE NUMBER OF YOUR RESPONSE: "))
if bag == 1:
print("You have chosen YES. We'll help with bags")
break
# Stop the loop as the option is valid
elif bag == 2:
print("Ok you don't want any help.")
break
elif bag == 3:
print("Tell us, when you want the help")
break
else:
print("Invalid Choice, Please select number from 1 to 3")
广告
数据结构
网络
关系型数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C 程序设计
C++
C#
MongoDB
MySQL
Javascript
PHP