ในโลกของการเรียนรู้การเขียนโปรแกรม มีหลายวิธีที่จะช่วยส่งเสริมและเติมเต็มทักษะของผู้เรียน หนึ่งในนั้นคือการสร้างโปรเจกต์ขนาดเล็กหรือ Mini Projects ซึ่งจะทำให้ผู้เรียนสามารถนำความรู้ไปประยุกต์ใช้ เพื่อสร้างผลงานที่มีชีวิตชีวาและมีประโยชน์ วันนี้เราจะมากล่าวถึง 5 โปรเจกต์พื้นฐานที่เขียนด้วย Python พร้อมแปะตัวอย่างโค้ด เพื่อเสนอแนวทางในการเริ่มลงมือทำตัวเอง!
#### 1. โปรแกรมคำนวณเครื่องคิดเลข
เครื่องคิดเลขเป็นโปรเจกต์ยอดนิยมสำหรับผู้เริ่มต้น มันช่วยสอนเกี่ยวกับการรับข้อมูลจากผู้ใช้ การประมวลผล เงื่อนไข และแสดงผลลัพธ์
def add(x, y):
return x + y
def subtract(x, y):
return x - y
def multiply(x, y):
return x * y
def divide(x, y):
return x / y
print("เลือกการดำเนินการ: ")
print("1.บวก")
print("2.ลบ")
print("3.คูณ")
print("4.หาร")
choice = input("ใส่ตัวเลือก(1/2/3/4): ")
num1 = float(input("ใส่ตัวเลขที่ 1: "))
num2 = float(input("ใส่ตัวเลขที่ 2: "))
if choice == '1':
print(num1, "+", num2, "=", add(num1, num2))
elif choice == '2':
print(num1, "-", num2, "=", subtract(num1, num2))
elif choice == '3':
print(num1, "*", num2, "=", multiply(num1, num2))
elif choice == '4':
print(num1, "/", num2, "=", divide(num1, num2))
else:
print("ใส่ตัวเลือกที่ถูกต้อง")
การสร้างเครื่องคิดเลขจะช่วยส่งเสริมความเข้าใจในพื้นฐานและการทำงานวนซ้ำ อย่างต่อเนื่อง จนกระทั่งโปรแกรมทำงานอย่างถูกต้อง
#### 2. โปรแกรมทายคำ (Hangman)
โปรเจกต์ทายคำนี้เป็นโปรเจกต์ที่ง่ายขึ้นหน่อยแต่มีความท้าทาย เพราะต้องคำนวณทางตรรกะและทำการจัดการกับสตริงส์และข้อมูล
import random
words_list = ['python', 'java', 'kotlin', 'javascript']
word = random.choice(words_list)
guessed = "_" * len(word)
tries = 8
print("H A N G M A N\n")
while tries > 0 and "_" in guessed:
print(guessed)
guess = input("Guess a letter: ").lower()
if guess in word and guess not in guessed:
guessed = "".join([guess if word[i] == guess else guessed[i] for i in range(len(word))])
else:
print("That letter doesn't appear in the word")
tries -= 1
print()
if "_" not in guessed:
print(f"You guessed the word {guessed}!\nYou survived!")
else:
print("You lost!")
โค้ดนี้แสดงให้เห็นถึงการสุ่มคำจากรายการ และให้ผู้เล่นทายตัวอักษรจนกว่าจะชนะ หรือแพ้เกม
#### 3. โปรแกรมสร้างไดอารี่บันทึกส่วนตัว
ไดอารี่ดิจิตอลเป็นโปรเจกต์ที่ดีในการฝึกฝนการจัดการกับไฟล์และข้อมูลของผู้ใช้
from datetime import datetime
def get_date_time():
return datetime.now().strftime("%Y%m%d_%H%M%S")
def save_entry(entry_text):
file_name = f"diary_{get_date_time()}.txt"
with open(file_name, "w") as file:
file.write(entry_text)
def read_diary():
prefix = "diary_"
suffix = ".txt"
for i in sorted(os.listdir('.')):
if i.startswith(prefix) and i.endswith(suffix):
with open(i, 'r') as f:
print(f.read())
entry = input("What's on your mind today?\n")
save_entry(entry)
print("Would you like to read your diary entries? (yes/no)")
if input().lower() == 'yes':
read_diary()
ตัวอย่างโค้ดนี้เป็นจุดเริ่มต้นที่ดีสำหรับโปรแกรมประเภทนี้ และมีที่เหลือเฟือเพื่อการปรับปรุงเพิ่มเติม
#### 4. เกม XO (Tic Tac Toe)
XO หรือ Tic Tac Toe เป็นเกมที่ต้องใช้การตัดสินใจและวางแผนการเล่นที่ดี เป็นการฝึกทักษะด้านเลขฐานและวิธีการในการออกแบบคณิตศาสตร์
def print_board(board):
for row in board:
print(" | ".join(row))
print("-" * 5)
def make_move(board, row, col, player):
if board[row][col] == " ":
board[row][col] = player
return True
return False
def check_winner(board, player):
# Check rows, columns and diagonals
for i in range(3):
if all([cell == player for cell in board[i]]) or \
all([board[j][i] == player for j in range(3)]) or \
all([board[j][j] == player for j in range(3)]) or \
all([board[j][2 - j] == player for j in range(3)]):
return True
return False
board = [[" "]*3 for _ in range(3)]
current_player = "X"
while True:
print_board(board)
try:
row, col = map(int, input(f"Player {current_player}, enter row and column (0, 1, 2): ").split())
if not make_move(board, row, col, current_player):
print("Invalid move, try again.")
continue
except ValueError:
print("Please enter two numbers separated by space.")
continue
if check_winner(board, current_player):
print(f"Player {current_player} wins!")
break
if all(cell != " " for row in board for cell in row):
print("It's a tie!")
break
current_player = "O" if current_player == "X" else "X"
การเขียนโปรแกรมเกมนี้จำเป็นต้องใช้ความเข้าใจในการทำงานของลูป ตรรกะต่างๆและการเข้าถึงข้อมูลในอาร์เรย์
#### 5. โปรแกรมดึงข้อมูลอากาศ (Web Scraping Weather Data)
การดึงข้อมูลจากเว็บไซต์และแปลงข้อมูลเป็นข้อมูลที่มีประโยชน์เป็นทักษะที่มีค่า
import requests
from bs4 import BeautifulSoup
def get_weather(city_name):
url = f"https://www.weather-forecast.com/locations/{city_name}/forecasts/latest"
r = requests.get(url)
soup = BeautifulSoup(r.text, 'html.parser')
weather = soup.find('span', attrs={'class': 'phrase'}).text
return weather
city = input("Enter a city name: ")
print(get_weather(city))
ด้วยโค้ดนี้ คุณสามารถรับข้อมูลด้านอากาศจากเว็บไซต์ต่าง ๆ ที่เข้าถึงได้ ซึ่งสามารถนำไปใช้เป็นโปรเจกต์ที่ให้ข้อมูลแก่ผู้ใช้ได้
จากข้างต้นเป็นตัวอย่างโปรเจกต์เพียง 5 อย่างที่สามารถช่วยพัฒนาทักษะการเขียนโค้ดและความเข้าใจในการทำงานของโปรแกรม Python ของคุณ โปรเจกต์เหล่านี้จะเป็นบันไดขั้นต้นที่ช่วยให้คุณก้าวไปสู่โปรเจกต์ที่ซับซ้อนมากขึ้นในอนาคตได้อย่างมั่นใจเลยทีเดียว และหากคุณต้องการผู้ช่วยเพื่อเรียนรู้การเขียนโปรแกรมที่มีประสิทธิภาพ หรือมองหาคู่มือที่ดีเพื่อพัฒนาทักษะของคุณ EPT ของเราพร้อมที่จะช่วยเหลือคุณในทุกๆ ด้านของการเขียนโปรแกรม Python.
หมายเหตุ: ข้อมูลในบทความนี้อาจจะผิด โปรดตรวจสอบความถูกต้องของบทความอีกครั้งหนึ่ง บทความนี้ไม่สามารถนำไปใช้อ้างอิงใด ๆ ได้ ทาง EPT ไม่ขอยืนยันความถูกต้อง และไม่ขอรับผิดชอบต่อความเสียหายใดที่เกิดจากบทความชุดนี้ทั้งทางทรัพย์สิน ร่างกาย หรือจิตใจของผู้อ่านและผู้เกี่ยวข้อง
หากเจอข้อผิดพลาด หรือต้องการพูดคุย ติดต่อได้ที่ https://m.me/expert.Programming.Tutor/
หากมีข้อผิดพลาด/ต้องการพูดคุยเพิ่มเติมเกี่ยวกับบทความนี้ กรุณาแจ้งที่ http://m.me/Expert.Programming.Tutor
085-350-7540 (DTAC)
084-88-00-255 (AIS)
026-111-618
หรือทาง EMAIL: NTPRINTF@GMAIL.COM