ไพธอนฉบับไม่ใช่โปรแกรมเมอร์ ตอน 5 (Decisions)

แปลตอนที่ห้าสำหรับ ไพธอนฉบับไม่ใช่โปรแกรมเมอร์ แปลสนุก ๆ ไว้อ่านเอง ตามไปอ่านตอนก่อนหน้าได้ที่นี่

แปลมาจาก Non-Programmer's Tutorial for Python

คำสั่ง If

ก็เหมือนที่ผ่านมา ผมจะเริ่มต้นบทนี้ด้วยการเตรียมความพร้อมด้านการพิมพ์ของคุณ จากแบบฝึกหัด และนี่คือ โปรแกรมสั้น ๆ ที่ คำนวนค่าสัมบูรณ์ ของตัวเลข

n = input("Number? ")
if n < 0:
   print "The absolute value of", n, "is", -n
else:
   print "The absolute value of", n, "is", n

และนี่คือผลลัพธ์ที่ได้หลังจากการรันโปรแกรมสองครั้ง

Number? -34
The absolute value of -34 is 34

Number? 1
The absolute value of 1 is 1

ดังนั้นอะไรคือสิ่งที่คอมพิวเตอร์ทำเมื่อเมื่อเห็นคำสั่ง เริ่มแรกเลยมันจะรอรับจำนวนจากผู้ใช้ด้วยคำสั่ง n = input("Number? ") ในบรรทัดต่อมาคำสั่ง if n<0 ถ้า n มีค่าน้อยกว่าศูนย์ ไพธอน จะทำงานในบรรทัด print "The absolute value of", n, "is", -n แต่ถ้าไม่ จะทำงานในบรรทัด print "The absolute value of", n, "is", n

โดยทั่วไป ไพธอนแค่มองว่านิพจน์ n < 0 เป็นจริงหรือเท็จ คำสั่ง if จะต้องถูกตามด้วย คำสั่งที่เยื้องไปทางขวา (indented block of statement) ซึ่งจะทำงานเมื่อ นิพจน์เป็นจริง บางครั้งต่อจาก คำสั่ง if จะมีคำสั่ง else และ คำสั่งที่เยื้องไปทางขวาอีกชุดหนึ่ง ซึ่ง คำสั่งชุดนี้จะถูกทำงานก็ต่อเมื่อนิพจน์เป็นเท็จ

และต่อไปนี้คือ ตัวดำเนินการเปลี่ยบเทียบที่สามารถใช้ได้

ตัวดำเนินการ การกระทำ
< น้อยกว่า
<= น้อยกว่าหรือเท่ากับ
> มากกว่า
>= มากกว่าหรือเท่ากับ
== เท่ากับ
!= ไม่เท่ากับ
<> ไม่เท่ากับ อีกวิธีหนึ่ง

คุณสมบัติหนึ่งของคำสั่ง IF คือ คำสั่ง elif มันมีค่าเหมือนกับคำสั่ง else และ if มันหมายความว่า ถ้า คำสั่ง if ตัวแรกเป็นเท็จ และนิพจน์ใน elif เป็นจริง แล้วจะทำงานใน ส่วนที่ต่อมา ดังตัวอย่าง

a = 0
while a < 10:
    a = a + 1
    if a > 5:
        print a, ">", 5
    elif a <= 7:
        print a, "<=", 7
    else:
        print "Neither test was true"

และนี่คือผลลัพธ์ที่ได้

1 <= 7
2 <= 7
3 <= 7
4 <= 7
5 <= 7
6 > 5
7 > 5
8 > 5
9 > 5
10 > 5

จงจำไว้ว่า คำสั่ง elif a <= 7 จะได้ทำการทดสอบเมื่อ นิพจน์ในคำสั่ง if ให้ผลลัพธ์เป็นเท็จและสามารถใช้คำสั่ง elif หลายคำสั่ง ต่อกันไปได้ หลังจากมีการใช้คำสั่ง if แล้วหนึ่งครั้ง

ตัวอย่าง

# This Program Demonstrates the use of the == operator
# using numbers
print 5 == 6
# Using variables
x = 5
y = 8
print x == y

และนี่คือผลลัพธ์

false
false

High_low.py

# Plays the guessing game higher or lower 
 
# This should actually be something that is semi random like the
# last digits of the time or something else, but that will have to
# wait till a later chapter.  (Extra Credit, modify it to be random
# after the Modules chapter)
number = 78
guess = 0
 
while guess != number: 
    guess = input ("Guess a number: ")
    if guess > number:
        print "Too high"
    elif guess < number:
        print "Too low"
 
print "Just right"

แนะนี่คือตัวอย่างการรัน

Guess a number: 100
Too high
Guess a number: 50
Too low
Guess a number: 75
Too low
Guess a number: 87
Too high
Guess a number: 81
Too high
Guess a number: 78
Just right

even.py

# Asks for a number.
# Prints if it is even or odd
 
number = input("Tell me a number: ")
if number % 2 == 0:
    print number, "is even."
elif number % 2 == 1:
    print number, "is odd."
else:
    print number, "is very strange."

ตัวอย่างการทำงาน

Tell me a number: 3
3 is odd.

Tell me a number: 2
2 is even.

Tell me a number: 3.14159
3.14159 is very strange.

average1.py

# keeps asking for numbers until 0 is entered.
# Prints the average value.
 
count = 0
sum = 0.0
number = 1 # set to something that will not exit the while loop immediatly.
 
print "Enter 0 to exit the loop"
 
while number != 0:
    number = input("Enter a number: ")
    if number != 0:
        count = count + 1
        sum = sum + number
 
print "The average was:", sum / count

ตัวอย่างการทำงาน

Enter 0 to exit the loop

Enter a number: 3
Enter a number: 5
Enter a number: 0
The average was: 4.0

Enter 0 to exit the loop
Enter a number: 1
Enter a number: 4
Enter a number: 3
Enter a number: 0
The average was: 2.66666666667

average2.py

# keeps asking for numbers until count numbers have been entered.
# Prints the average value.
 
sum = 0.0
 
print "This program will take several numbers then average them"
count = input("How many numbers would you like to average: ")
current_count = 0
 
while current_count < count:
    current_count = current_count + 1
    print "Number", current_count
    number = input("Enter a number: ")
    sum = sum + number
 
print "The average was:", sum / count

ตัวอย่างการทำงาน

This program will take several numbers then average them

How many numbers would you like to average: 2
Number 1
Enter a number: 3
Number 2
Enter a number: 5
The average was: 4.0

This program will take several numbers then average them
How many numbers would you like to average: 3
Number 1
Enter a number: 1
Number 2
Enter a number: 4
Number 3
Enter a number: 3
The average was: 2.66666666667

แบบฝึกหัด

  1. แก้ไขโปรแกรมทายตัวเลขในบทนี้ (higher or lower program) ให้มีการนับจำนวนครั้งการทายไว้ด้วย และเมื่อทายถูก ถ้ามีจำนวนครั้งที่ทายผิดมากกว่า 3 ครั้ง ให้พิมพ์ print "That must have been cmoplicated"
  2. เขียนโปรแกรมรับค่าจำนวนสองจำนวนจากผู้ใช้ ถ้าจำนวนนั้นรวมกันมีค่ามากกว่าร้อย ให้แสดงผลลัพธ์ว่า print "That is a big number."
  3. เขียนโปรแกรมรับชื่อของผู้ใช้ ถ้าชื่อนั้นตรงกับชื่อของคุณให้พิมพ์ว่า That is a nice name. ถ้าชื่อนั้นตรงกับคำว่า "John Cleese" หรือ "Michael Palin" ให้บอกว่า Good jobs. และถ้าเป็นคนอื่นให้พิมพ์ว่า You have a nice name.

เรื่องที่เกี่ยวข้อง

Leave a Reply

You must be logged in to post a comment.