使用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")热门推荐
10 圣诞祝福语简短小学
11 祖国七十华诞简短祝福语
12 老师送的祝福语简短
13 生日祝福语大全女生简短
14 祝女性生日祝福语简短
15 牛年女神节祝福语简短
16 情人表白祝福语简短大气
17 老公开业祝福语简短
18 官宣新年祝福语简短