使用 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