forked from steam-bell-92/python-mini-project
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidation.py
More file actions
67 lines (65 loc) · 2.7 KB
/
Copy pathvalidation.py
File metadata and controls
67 lines (65 loc) · 2.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
from typing import List, Optional
def get_int(
prompt: str,
min_value: Optional[int] = None,
max_value: Optional[int] = None,
default: Optional[int] = None,
error_empty: str = "❌ Error: Input cannot be empty.",
error_invalid: str = "❌ Invalid input. Please enter a valid integer.",
) -> int:
while True:
try:
val_str = input(prompt).strip()
if not val_str:
if default is not None:
if min_value is not None and default < min_value:
print(f"❌ Default {default} is below minimum {min_value}.")
continue
if max_value is not None and default > max_value:
print(f"❌ Default {default} is above maximum {max_value}.")
continue
return default
print(error_empty)
continue
val = int(val_str)
if min_value is not None and val < min_value:
print(f"❌ Please enter a number greater than or equal to {min_value}.")
continue
if max_value is not None and val > max_value:
print(f"❌ Please enter a number less than or equal to {max_value}.")
continue
return val
except ValueError:
print(error_invalid)
def get_float(
prompt: str,
min_value: Optional[float] = None,
max_value: Optional[float] = None,
default: Optional[float] = None,
error_empty: str = "❌ Error: Input cannot be empty.",
error_invalid: str = "❌ Invalid input. Please enter a valid number.",
) -> float:
while True:
try:
val_str = input(prompt).strip()
if not val_str:
if default is not None:
if min_value is not None and default < min_value:
print(f"❌ Default {default} is below minimum {min_value}.")
continue
if max_value is not None and default > max_value:
print(f"❌ Default {default} is above maximum {max_value}.")
continue
return default
print(error_empty)
continue
val = float(val_str)
if min_value is not None and val < min_value:
print(f"❌ Please enter a number greater than or equal to {min_value}.")
continue
if max_value is not None and val > max_value:
print(f"❌ Please enter a number less than or equal to {max_value}.")
continue
return val
except ValueError:
print(error_invalid)