เงื่อนไข if และ if elif
if เงื่อนไข :
คำสั่งต่างๆ
ถ้าใช้ร่วมกับ elif ก็จะใช้แบบนี้นะ
if เงื่อนไข :
คำสั่งต่างๆ
elif เงื่อนไข :
คำสั่งต่างๆ
ตัวอย่างโค้ด
number=input("Please Enter number 1-10 : ")
if int(number)>5 :
print("You Entered number greater than 5")
elif int(number)<=5 :
print("You Entered number less than 6")
ตัวอย่างโค้ด 2 การใช้ if elif และ else
number=input("Please enter number 1-12 : ")
if int(number)>6 and int(number)<13 :
print("Hi")
elif int(number)==6 :
print("Middle")
elif int(number)>0 and int(number)<6 :
print("Lo")
else :
print("You entered the fucking wrong number.I said 1-12 bitch!!")
การใช้ switch ในภาษา python
เนื่องจาก python ไม่มี switch case จึงใช้ dictionary ในการ switch ข้อมูลแทน ดังตัวอย่าง
def numbers_to_strings(argument):
switcher = {
0: "zero",
1: "one",
2: "two",
}
return switcher.get(argument, "nothing")
print(numbers_to_strings(1))
แหล่งที่มาแอบอ้าง http://www.tutorialspoint.com/python/python_dictionary.htm