-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexe_093_centered_string.py
59 lines (42 loc) · 1.8 KB
/
exe_093_centered_string.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
"""
Write a function that takes a string, s, as its first parameter, and
the width of the window in characters, w, as its second parameter.
Your function will return a new string that includes
whatever leading spaces are needed
so that s will be centered in the window when the new string is printed.
The new string should be constructed in the following manner:
- if the length of s is greater than or equal to the width of the window then s should be returned
- if the length of s is less than the width of the window
then a string containing (len(s) - w) // 2 spaces followed by s should be returned.
Write a main program that demonstrates your function by displaying multiple strings centered in the window.
"""
# START Definition of FUNCTIONS
def valutaEntry(number): # Possible evolution -> IMPORT module
# Check Entry -> INT POSITIVE
return True
def centerString(s, w):
w = int(w)
if len(s) >= w:
return s
else:
spaces = (w - len(s)) // 2
return (" " * spaces) + s + (" " * spaces)
# END Definition of FUNCTIONS
# START MAIN PROGRAM
def main():
# Acquisition and Control of the DATA entered by the USER
print("Enter the STRING and the WIDTH of the WINDOWS.")
string = input("STRING: ")
width_window = input("WIDTH (number of characters): ")
width_window_validated = valutaEntry(width_window)
while not(width_window_validated):
print("Incorrect entry. Try again.")
print("Enter the STRING and the WIDTH of the WINDOWS.")
string = input("STRING: ")
width_window = input("WIDTH (number of characters): ")
width_window_validated = valutaEntry(width_window)
# Displaying the RESULTS
result = centerString(string, width_window)
print("|" + result + "|")
if __name__ == "__main__":
main()