使用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")热门推荐
4 足球队祝福语简短
10 晚儿子生日祝福语简短
11 商业mc祝福语大全简短
12 表达朋友的祝福语简短
13 元旦的祝福语简短 公司
14 朋友孩子考试祝福语简短
15 官宣新年祝福语简短
16 关于好的祝福语简短
17 送给老师中秋祝福语简短
18 新生祝福语简短8字