Reply to user text using Python


You can solve this problem by using if-elif-else statements. And to make it like, it will ask for a valid option until the given option is on the list, we can use while loops. When the option is valid, then break the loop, otherwise, it will ask for the input repeatedly.

You should take the input as an integer, for that you need to typecast the input to an integer using int() method.

Example

Please check the code to follow the given points.

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")

Updated on: 16-Jun-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements