Counting loops · วนซ้ำแบบนับจำนวน
Repeat with for
- A
forloop repeats code a fixed number of times. - The body of the loop is indented, like an
if. - A loop variable holds a new value on each pass.
ทำซ้ำด้วย for
- ลูป
forจะทำซ้ำโค้ดจำนวนครั้งที่กำหนด - ร่างของลูปจะถูก ย่อหน้า เหมือนกับ
if - ตัวแปรลูป จะถือค่าใหม่ในแต่ละรอบ
Counting with range
range(5)gives the numbers0, 1, 2, 3, 4(stops before 5).range(1, 6)gives1, 2, 3, 4, 5(start included, stop not).range(0, 10, 2)counts in steps of 2.
การนับด้วย range
range(5)ให้ตัวเลข0, 1, 2, 3, 4(หยุดก่อน 5)range(1, 6)gives1, 2, 3, 4, 5(start included, stop not).range(0, 10, 2)นับโดยมี ขั้น ของ 2
for i in range(5):
print(i)
Build a total (accumulator)
- An accumulator is a variable that grows inside a loop.
- Start it at
0before the loop. - Add to it on each pass to build a running total.
สร้างผลรวม (accumulator)
- Accumulator คือตัวแปรที่เพิ่มขึ้นภายใน loop
- เริ่มที่
0ก่อน loop - เพิ่มเข้าไปในรอบละครั้งเพื่อสร้างผลรวมสะสม
total = 0
for n in range(1, 6):
total = total + n
print(total)
In Cambridge pseudocode
- A Python
for … rangeloop is the exam's count-controlledFOR … NEXT.
ใน伪代码 Cambridge
- Python
for … rangeloop คือ count-controlledFOR … NEXTของการสอบ
Total ← 0
FOR I ← 1 TO 5
Total ← Total + I
NEXT I
OUTPUT Total
Words and a number on one line
- A comma inside
printputs a space between the parts. print("7 x", 1, "=", 7 * 1)prints7 x 1 = 7.+cannot join a number to text. Use the comma, or turn the number into text withstr.
คำและตัวเลขบนบรรทัดเดียว
- A comma inside
printputs a space between the parts. print("7 x", 1, "=", 7 * 1)พิมพ์7 x 1 = 7+ไม่สามารถนำตัวเลขมาต่อกับข้อความได้ ให้ใช้ comma หรือแปลงตัวเลขให้เป็นข้อความด้วยstr
Common mistakes
range(n)counts0ton - 1, not1ton.range(a, b)stops beforeb.- The loop body must be indented under the
forline.
ข้อผิดพลาดที่พบบ่อย
range(n)นับจาก0ถึงn - 1ไม่ใช่1ถึงnrange(a, b)หยุด ก่อนb- ร่างของลูปต้องถูกย่อหน้าอยู่ใต้บรรทัด
for
Now you try
- Use a
forloop withrange(...)in each task. - Press Check answer to test your code.
ลองดูเลย
- ใช้
forloop พร้อมrange(...)ในแต่ละงาน - กด Check answer เพื่อทดสอบโค้ดของคุณ
A counting loop, pass by pass · วนซ้ำแบบนับจำนวน, ทีละขั้นตอน
A for loop repeats its body; watch the counter and the running total. · ลูป for จะทำส่วนในลูปซ้ำ; ให้สังเกตตัวนับและ ผลรวมสะสม
Use a for loop to print the numbers 1 to 5, each on its own line. · ใช้ for loop เพื่อพิมพ์ตัวเลข 1 ถึง 5, แต่ละตัวอยู่บนบรรทัดของตัวเอง
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
Use a loop and an accumulator to add up every whole number from 1 to 100. Store the total in total. · ใช้ลูปและตัวสะสมเพื่อบวกเลขจำนวนเต็มตั้งแต่ 1 ถึง 100. เก็บผลรวมไว้ใน total
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
Multiply together every whole number from 1 to 5 (that is 1×2×3×4×5). Start product at 1 and store the result there. · คูณกันทุกจำนวนเต็มตั้งแต่ 1 ถึง 5 (นั่นคือ 1×2×3×4×5). เริ่มต้น product ที่ 1 และเก็บผลลัพธ์ไว้ที่那里.
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
Print exactly one line: 7 x 1 = 7. Finish the print that is already started. A comma between the parts prints them with spaces: print("7 x", 1, "=", 7 * 1). · พิมพ์บรรทัดเดียวเพียงบรรทัดเดียว: 7 x 1 = 7. เสร็จสิ้น print ที่เริ่มไว้แล้ว. การวางลูกน้ำระหว่างส่วนต่างๆ จะทำให้พิมพ์ด้วยช่องว่าง: print("7 x", 1, "=", 7 * 1)
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
Use a for loop to print the first five lines of the 7 times table: 7 x 1 = 7, 7 x 2 = 14, up to 7 x 5 = 35. The print from the last task is already inside the loop — change the 1 into the loop variable. · ใช้ลูป for เพื่อพิมพ์ห้าบรรทัดแรกของตารางคูณ 7: 7 x 1 = 7, 7 x 2 = 14, ไปจนถึง 7 x 5 = 35. ตัวแปร print จากงานก่อนหน้าอยู่ในลูปอยู่แล้ว — เปลี่ยน 1 ให้เป็นตัวแปรของลูป.
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่