Skip to content

Commit 7e24430

Browse files
authored
Merge branch 'master' into python-del-statement
2 parents 019f13a + 818c59f commit 7e24430

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

85 files changed

+5197
-0
lines changed

python-input-integer/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# How to Read Python Input as Integers
2+
3+
Here you can find code examples for the tutorial [How to Read Python Input as Integers](https://realpython.com/python-input-integer/).
4+
5+
The file [`number_input.py`](number_input.py) contains the `get_integer()` function discussed in the tutorial plus some other utility functions for numeric input.
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
"""Utility functions for prompting users for numeric input.
2+
3+
This file accompanies the Real Python tutorial How To Read User Input As
4+
Integers. It contains examples of functions that filter user input for valid
5+
integers and floats.
6+
"""
7+
8+
9+
def get_integer(prompt: str, error_message: str = None) -> int:
10+
"""Prompts the user for an integer value.
11+
12+
If the input is invalid, prints error_message (if specified) and then
13+
repeats the prompt.
14+
"""
15+
while True:
16+
try:
17+
return int(input(prompt))
18+
except ValueError:
19+
if error_message:
20+
print(error_message)
21+
22+
23+
def get_float(prompt: str, error_message: str = None) -> float:
24+
"""Prompts the user for a float value.
25+
26+
If the input is invalid, prints error_message (if specified) and then
27+
repeats the prompt.
28+
"""
29+
while True:
30+
try:
31+
return float(input(prompt))
32+
except ValueError:
33+
if error_message:
34+
print(error_message)
35+
36+
37+
def get_integer_with_default(
38+
prompt: str, default_value: int, error_message: str = None
39+
) -> int:
40+
"""Prompts the user for an integer, and falls back to a default value.
41+
42+
If the input is an empty string, then the function returns default_value.
43+
Otherwise, if the input is not a valid integer, prints error_message (if
44+
specified) and then repeats the prompt.
45+
"""
46+
while True:
47+
input_string = input(prompt)
48+
if not input_string:
49+
return default_value
50+
try:
51+
return int(input_string)
52+
except ValueError:
53+
if error_message:
54+
print(error_message)
55+
56+
57+
if __name__ == "__main__":
58+
# Test functions when running as a script.
59+
print(get_integer("Test get_integer(): "))
60+
print(
61+
get_integer(
62+
"Test get_integer() with an error message: ",
63+
error_message="Invalid integer!",
64+
)
65+
)
66+
print(
67+
get_float(
68+
"Test get_float() with an error message: ",
69+
error_message="Invalid float!",
70+
)
71+
)
72+
print(
73+
get_integer_with_default(
74+
"Test get_integer_with_default(): ",
75+
default_value=99,
76+
error_message="That's not a valid integer!",
77+
)
78+
)

python-maze-solver/README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Build a Maze Solver in Python Using Graphs
2+
3+
This is the code for the Real Python tutorial [Build a Maze Solver in Python Using Graphs](https://realpython.com/python-maze-solver/).
4+
5+
## Installation
6+
7+
The code requires Python 3.10 or later and depends on the NetworkX library. You can install the project into an active virtual environment by issuing the following command from the project root folder:
8+
9+
```sh
10+
(venv) $ python -m pip install .
11+
```
12+
13+
For development, consider installing the code in editable mode:
14+
15+
```sh
16+
(venv) $ python -m pip install --editable .
17+
```
18+
19+
## Running
20+
21+
To run the maze solver, use the `solve` command and provide the path to a maze file:
22+
23+
```sh
24+
$ solve /path/to/labyrinth.maze
25+
```
26+
27+
Alternatively, you can use the more explicit yet equivalent command:
28+
29+
```sh
30+
$ python -m maze_solver /path/to/labyrinth.maze
31+
```
32+
33+
If the command succeeds, then you should see either a rendered solution in your default web browser or a relevant error message in the console.
28 Bytes
Binary file not shown.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# fmt: off
2+
# flake8: noqa
3+
from pathlib import Path
4+
5+
from maze_solver.models.border import Border
6+
from maze_solver.models.maze import Maze
7+
from maze_solver.models.role import Role
8+
from maze_solver.models.square import Square
9+
10+
11+
def main() -> None:
12+
build_maze().dump(Path(__file__).with_suffix(".maze"))
13+
14+
15+
def build_maze() -> Maze:
16+
return Maze(
17+
squares=(
18+
Square(0, 0, 0, Border.EMPTY, Role.EXTERIOR),
19+
Square(1, 0, 1, Border.TOP | Border.LEFT),
20+
Square(2, 0, 2, Border.TOP | Border.BOTTOM),
21+
Square(3, 0, 3, Border.TOP | Border.RIGHT),
22+
Square(4, 0, 4, Border.BOTTOM | Border.LEFT, Role.EXTERIOR),
23+
Square(5, 1, 0, Border.TOP | Border.BOTTOM | Border.RIGHT, Role.EXIT),
24+
Square(6, 1, 1, Border.LEFT | Border.RIGHT),
25+
Square(7, 1, 2, Border.TOP | Border.BOTTOM | Border.LEFT | Border.RIGHT, Role.WALL),
26+
Square(8, 1, 3, Border.LEFT),
27+
Square(9, 1, 4, Border.TOP | Border.BOTTOM, Role.ENTRANCE),
28+
Square(10, 2, 0, Border.TOP | Border.RIGHT, Role.EXTERIOR),
29+
Square(11, 2, 1, Border.BOTTOM | Border.LEFT),
30+
Square(12, 2, 2, Border.TOP | Border.BOTTOM),
31+
Square(13, 2, 3, Border.BOTTOM | Border.RIGHT),
32+
Square(14, 2, 4, Border.TOP | Border.LEFT, Role.EXTERIOR),
33+
)
34+
)
35+
36+
37+
if __name__ == '__main__':
38+
main()
909 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)