forked from TheAlgorithms/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhollow_diamond_alphabet.py
83 lines (65 loc) · 2.34 KB
/
hollow_diamond_alphabet.py
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
def hollow_diamond_alphabet(n):
"""
Prints a hollow diamond pattern using uppercase alphabet characters.
:param n: An integer representing the number of rows in the diamond.
:return: True if the pattern was successfully printed, False otherwise.
"""
if not isinstance(n, int):
print("Error: Input must be an integer.")
return False
if n <= 0:
print("Error: Input must be a positive integer.")
return False
if n % 2 == 0:
print("Error: Input must be an odd integer.")
return False
# Calculate the number of rows for the upper half of the diamond
upper_rows = (n + 1) // 2
# Print the upper half of the diamond
for i in range(upper_rows):
char = chr(65 + i) # Convert number to uppercase alphabet
if i == 0:
print(" " * (upper_rows - 1) + char)
else:
print(" " * (upper_rows - i - 1) + char + " " * (2 * i - 1) + char)
# Print the lower half of the diamond
for i in range(upper_rows - 2, -1, -1):
char = chr(65 + i) # Convert number to uppercase alphabet
if i == 0:
print(" " * (upper_rows - 1) + char)
else:
print(" " * (upper_rows - i - 1) + char + " " * (2 * i - 1) + char)
return True
def get_valid_input():
"""
Prompts the user for input and validates it.
:return: A valid positive odd integer, or None if the user chooses to quit.
"""
while True:
user_input = input(
"Enter the diamond size (positive odd integer) or 'q' to quit: "
)
if user_input.lower() == "q":
return None
try:
n = int(user_input)
if n > 0 and n % 2 != 0:
return n
elif n <= 0:
print("Error: Please enter a positive integer.")
else:
print("Error: Please enter an odd integer.")
except ValueError:
print("Error: Invalid input. Please enter a valid integer.")
# Main program
def main():
while True:
size = get_valid_input()
if size is None:
print("Thank you!")
break
if hollow_diamond_alphabet(size):
print("\nDiamond pattern printed successfully!")
print() # Add a blank line for better readability
if __name__ == "__main__":
main()