Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0a401e6

Browse files
Created new beginner- and intermediate level projects.
1 parent 5496b2a commit 0a401e6

File tree

3 files changed

+198
-2
lines changed

3 files changed

+198
-2
lines changed

‎Beginner/7_word_counter.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Get the input file path (TXT) from the user
2+
file_path = input("Enter the file path (TXT): ")
3+
4+
# Read the file's content
5+
with open(file_path, 'r', encoding='UTF-8') as file:
6+
content = file.read()
7+
8+
# Split the content into words
9+
words = content.split()
10+
11+
# Count the number of words
12+
word_count = len(words)
13+
14+
# Display the result
15+
print(f"The file contains {word_count} words.")

‎Intermediate/1_tic_tac_toe.py

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
import random
2+
3+
# Display the board
4+
def display_board(board):
5+
display = []
6+
for idx, cell in enumerate(board, 1):
7+
display.append(str(idx) if cell == ' ' else cell)
8+
print(f"{display[0]} | {display[1]} | {display[2]}")
9+
print("---------")
10+
print(f"{display[3]} | {display[4]} | {display[5]}")
11+
print("---------")
12+
print(f"{display[6]} | {display[7]} | {display[8]}")
13+
14+
# Check if a player has won
15+
def check_win(board, player):
16+
winning_combinations = [
17+
[0, 1, 2], [3, 4, 5], [6, 7, 8], # Rows
18+
[0, 3, 6], [1, 4, 7], [2, 5, 8], # Columns
19+
[0, 4, 8], [2, 4, 6] # Diagonals
20+
]
21+
for combination in winning_combinations:
22+
if (board[combination[0]] == player and
23+
board[combination[1]] == player and
24+
board[combination[2]] == player):
25+
return True
26+
return False
27+
28+
# Check if the board is full
29+
def is_board_full(board):
30+
return ' ' not in board
31+
32+
# Validate the move
33+
def validate_move(board, position):
34+
if not position.isdigit():
35+
return False
36+
position = int(position) - 1
37+
if position < 0 or position > 8:
38+
return False
39+
if board[position] != ' ':
40+
return False
41+
return True
42+
43+
# Get the player's move
44+
def get_human_move(board):
45+
while True:
46+
position = input("Enter position (1-9): ")
47+
if validate_move(board, position):
48+
return int(position) - 1
49+
else:
50+
print("Invalid move. Try again.")
51+
52+
# Get the computer's move
53+
def get_computer_move(board):
54+
# Check for immediate winning move
55+
empty_spots = [i for i, cell in enumerate(board) if cell == ' ']
56+
for spot in empty_spots:
57+
board[spot] = 'O'
58+
if check_win(board, 'O'):
59+
board[spot] = ' '
60+
return spot
61+
board[spot] = ' '
62+
63+
# Check for blocking the player's winning move
64+
for spot in empty_spots:
65+
board[spot] = 'X'
66+
if check_win(board, 'X'):
67+
board[spot] = ' '
68+
return spot
69+
board[spot] = ' '
70+
71+
# Priority strategy: center, corners, edges
72+
priority_order = [4, 0, 2, 6, 8, 1, 3, 5, 7]
73+
for spot in priority_order:
74+
if board[spot] == ' ':
75+
return spot
76+
77+
# Fallback to random choice if no spots left (should not happen)
78+
return random.choice(empty_spots)
79+
80+
def main():
81+
board = [' ' for _ in range(9)]
82+
current_player = 'X'
83+
game_over = False
84+
85+
while not game_over:
86+
display_board(board)
87+
print(f"Player {current_player}'s turn.")
88+
89+
if current_player == 'X':
90+
move = get_human_move(board)
91+
else:
92+
print("Computer's turn...")
93+
move = get_computer_move(board)
94+
95+
board[move] = current_player
96+
97+
if check_win(board, current_player):
98+
display_board(board)
99+
print(f"Player {current_player} wins!")
100+
game_over = True
101+
else:
102+
if is_board_full(board):
103+
display_board(board)
104+
print("It's a tie!")
105+
game_over = True
106+
else:
107+
current_player = 'O' if current_player == 'X' else 'X'
108+
109+
if __name__ == "__main__":
110+
main()

‎README.md

Lines changed: 73 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ This repository is designed to help Python learners at all levels, starting with
77
## Table of Contents
88
1. [Setup](#setup)
99
2. [Beginner Projects](#beginner-projects)
10-
3. [Contributing](#contributing)
11-
4. [License](#license)
10+
3. [Intermediate Projects](#intermediate-projects)
11+
4. [Contributing](#contributing)
12+
5. [License](#license)
1213

1314
## Setup
1415

@@ -217,9 +218,79 @@ These projects are ideal for those new to Python. Each project includes a descri
217218

218219
</details>
219220

221+
### 7. Word Counter
222+
- **Description**: Build a simple word counter.
223+
224+
- **Solution**: https://github.com/Infinitode/Python-Projects/blob/main/Beginner/7_word_counter.py
225+
226+
- **Steps**:
227+
1. Prompt the user for a file path (TXT).
228+
2. Read the file path.
229+
3. Count the words in the read data.
230+
4. Display the result.
231+
232+
- **Tips:**
233+
234+
</summary>
235+
<details><summary>Tip 1:</summary>
236+
237+
Use `input()` for input.
238+
239+
</details>
240+
<details><summary>Tip 2:</summary>
241+
242+
Read the file using `read operations` (https://www.geeksforgeeks.org/how-to-read-from-a-file-in-python/) in Python.
243+
244+
</details>
245+
<details><summary>Tip 3:</summary>
246+
247+
Count the number of words using `split()` and `len()`.
248+
249+
</details>
250+
<details><summary>Tip 4:</summary>
251+
252+
Print out the result using `print()`.
253+
254+
</details>
255+
220256
> [!NOTE]
221257
> Working code solutions are in the `/Beginner` folder.
222258
259+
## Intermediate Projects
260+
These projects are ideal for those with experience in Python. Each project includes a description, steps to follow, and tips for completing it.
261+
262+
### 1. Tic Tac Toe
263+
- **Description**: Build a tic tac toe board, with it's own algorithm for winning and countering player moves.
264+
265+
- **Solution**: https://github.com/Infinitode/Python-Projects/blob/main/Intermediate/1_tic_tac_toe.py
266+
267+
- **Steps**:
268+
1. Create the board (3x3 grid) and other logic (handling invalid moves, etc.)
269+
2. Handle player and computer moves, including ties, wins and losses.
270+
3. Display the board and start the game.
271+
272+
- **Tips:**
273+
274+
</summary>
275+
<details><summary>Tip 1:</summary>
276+
277+
Use functions for repetitive tasks.
278+
279+
</details>
280+
<details><summary>Tip 2:</summary>
281+
282+
Create an algorithm that not only chooses certain priority positions on the board, but also counters player moves.
283+
284+
</details>
285+
<details><summary>Tip 3:</summary>
286+
287+
Add error handling and retries if a player chooses a place on the board that is already occupied.
288+
289+
</details>
290+
291+
> [!NOTE]
292+
> Working code solutions are in the `/Intermediate` folder.
293+
223294
## Contributing
224295
Contributions are welcome! If you have project ideas or improvements, feel free to fork the repository and open a pull request.
225296

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /