Computer >> คอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

ตอบกลับข้อความของผู้ใช้โดยใช้ Python


คุณสามารถแก้ปัญหานี้ได้โดยใช้คำสั่ง if-elif-else และเพื่อให้เป็นเช่นนั้น มันจะขอตัวเลือกที่ถูกต้องจนกว่าตัวเลือกที่กำหนดจะอยู่ในรายการ เราสามารถใช้ while loops ได้ เมื่อตัวเลือกถูกต้อง ให้แตกลูป ไม่เช่นนั้นจะถามหาอินพุตซ้ำๆ

คุณควรรับอินพุตเป็นจำนวนเต็ม เพื่อที่คุณจะต้องพิมพ์อินพุตเป็นจำนวนเต็มโดยใช้เมธอด 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")