File tree Expand file tree Collapse file tree 2 files changed +59
-0
lines changed Expand file tree Collapse file tree 2 files changed +59
-0
lines changed Original file line number Diff line number Diff line change
1
+ def Square (s ):
2
+ for i in range (s ):
3
+ if i == 0 : # First row
4
+ print ('*' )
5
+ elif i == s - 1 : # Last row
6
+ print ('* ' * s )
7
+ else : # Middle rows
8
+ print ('*' + ' ' * (2 * i - 1 ) + '*' )
9
+ def check_length ():
10
+ while True :
11
+ length = input ('Enter the length of the triangle: ' )
12
+ if length .isdigit ():
13
+ length = int (length )
14
+ if length > 1 :
15
+ Square (length )
16
+ else :
17
+ print ('Length must be greater than one.' )
18
+ else :
19
+ print ('Please enter a digit.' )
20
+
21
+
22
+ check_length ()
23
+
24
+ if __name__ == '__main__' :
25
+ check_length ()
26
+
27
+
Original file line number Diff line number Diff line change
1
+ # simple square
2
+ def accept_square_size (): # A function to accept square size
3
+ size = input ('Enter the size of the square: ' )
4
+ return size
5
+
6
+
7
+ def validate_square_size (): # A function to validate square size
8
+ size = accept_square_size ()
9
+
10
+ if size .isdigit ():
11
+ size = int (size )
12
+ if size > 1 :
13
+ draw_square (size )
14
+ else :
15
+ print ('Size must be greater than one!' )
16
+ else :
17
+ print ('Size must be a digit!' )
18
+
19
+ def draw_square (size ): # A function to draw a square
20
+
21
+ for i in range (size ):
22
+ if i == size - 1 or i == 0 :
23
+ print ('* ' * size )
24
+ else :
25
+ print ('* ' + ' ' * (size - 2 )+ '*' )
26
+
27
+ def display_square (): # A function that displays the square
28
+ validate_square_size ()
29
+
30
+
31
+ if __name__ == '__main__' : # main function
32
+ display_square ()
You can’t perform that action at this time.
0 commit comments