File tree Expand file tree Collapse file tree 1 file changed +56
-19
lines changed Expand file tree Collapse file tree 1 file changed +56
-19
lines changed Original file line number Diff line number Diff line change 1- # pattern
2- # @@@@@@@@ $
3- # @@@@@@@ $$
4- # @@@@@@ $$$
5- # @@@@@ $$$$
6- # @@@@ $$$$$
7- # @@@ $$$$$$
8- # @@ $$$$$$$
9- # @ $$$$$$$$
1+ def main ():
2+ """Main function that gets user input and calls the pattern generation function."""
3+ try :
4+ lines = int (input ("Enter number of lines: " ))
5+ if lines < 0 :
6+ raise ValueError ("Number of lines must be non-negative" )
7+ result = pattern (lines )
8+ print (result )
9+ except ValueError as e :
10+ if "invalid literal" in str (e ):
11+ print (f"Error: Please enter a valid integer number. { e } " )
12+ else :
13+ print (f"Error: { e } " )
14+ except Exception as e :
15+ print (f"An unexpected error occurred: { e } " )
1016
1117
12- def main ():
13- lines = int (input ("Enter no.of lines: " ))
14- pattern (lines )
18+ def pattern (lines : int ) -> str :
19+ """
20+ Generate a pattern of '@' and '$' characters.
21+
22+ Args:
23+ lines: Number of lines to generate in the pattern
24+
25+ Returns:
26+ A multiline string containing the specified pattern
27+
28+ Raises:
29+ ValueError: If lines is negative
30+
31+ Examples:
32+ >>> print(pattern(3))
33+ @@@ $
34+ @@ $$
35+ @ $$$
36+
37+ >>> print(pattern(1))
38+ @ $
1539
40+ >>> print(pattern(0))
41+ <BLANKLINE>
1642
17- def pattern (lines ):
18- t = 1
43+ >>> pattern(-5)
44+ Traceback (most recent call last):
45+ ...
46+ ValueError: Number of lines must be non-negative
47+ """
48+ if lines < 0 :
49+ raise ValueError ("Number of lines must be non-negative" )
50+ if lines == 0 :
51+ return ""
52+
53+ pattern_lines = []
1954 for i in range (lines , 0 , - 1 ):
20- nxt_pattern = "$ " * t
21- pattern = "@ " * (i )
22- final_pattern = pattern + " " + nxt_pattern
23- print ( final_pattern )
24- t = t + 1
55+ at_pattern = "@ " * i
56+ dollar_pattern = "$ " * (lines - i + 1 )
57+ pattern_lines . append ( f" { at_pattern } { dollar_pattern } " )
58+
59+ return " \n " . join ( pattern_lines )
2560
2661
2762if __name__ == "__main__" :
63+ import doctest
64+ doctest .testmod (verbose = True )
2865 main ()
You can’t perform that action at this time.
0 commit comments