Skip to content

Commit 16712ff

Browse files
committed
valid ipv4 code
1 parent 92b207b commit 16712ff

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

test_valid_ipv4.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#A valid IPv4 address is a string that contains
2+
# four decimal numbers separated by periods,
3+
# where each decimal number is between 0 and 255.
4+
5+
def is_valid_IPv4(ip_address):
6+
# Split the IP address string into a list of 4 decimal numbers
7+
parts = ip_address.split(".")
8+
9+
# Check that there are exactly 4 parts
10+
if len(parts) != 4:
11+
return False
12+
13+
# Check that each part is an integer between 0 and 255
14+
for part in parts:
15+
try:
16+
# Convert each part to an integer
17+
num = int(part)
18+
except ValueError:
19+
# If a part cannot be converted to an integer, the address is not valid
20+
return False
21+
22+
# Check that the integer is between 0 and 255
23+
if num < 0 or num > 255:
24+
return False
25+
26+
# If all checks pass, the address is valid
27+
return True

0 commit comments

Comments
 (0)