使用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 教师闺蜜祝福语简短