In: Computer Science
'''
string_funcs
==========
Complete the following functions. You MUST, MUST, MUST add:
1) a doc comment in the proper location for EACH function that
describes
the basic purpose of the function.
2) AT LEAST 3 doc test comments in EACH function that:
* test that the function does what it is supposed to do
* tests that it does what it's supposed to do with odd inputs
* tests "edge" cases (numbers at, just above, just below min/max,
empty strings, etc.)
You MUST, MUST, MUST then test each of your methods by BOTH:
1) running the "main.py" script
2) running this module directly to run the doc tests
Except as noted, you can implement the functions however you
like. You can use string
functions OR f-strings to accomplish the formatting.
CHALLENGE: Use try/except blocks to avoid crashes when passing in unexpected parameters.
IMPORTANT!!! MIMIR RUNS PYTHON 3.5 WHEN YOU RUN python3.
F-STRINGS ARE NEW TO PYTHON
3.6 AND ABOVE. IF TESTING ON MIMIR, YOU MUST RUN python3.6 INSTEAD
OF JUST python3
IF YOU USE F-STRINGS.
print_header
------------
Print out a header line that prints the "text" parameter such that
it is centered and
takes up a total of 60 characters. It should then print a second
line that contains
60 "+" symbols. In other words, calling:
print_header('2021 Crop Report')
should print as follows:
2021 Crop Report
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
print_integer_line
------------------
Print out a line of text where the "text" parameter is left aligned
and takes up
a total of 50 characters and the "value" parameter is right aligned
and takes up
10 characters. In other words, calling:
print_integer_line('Fall 2020 Enrollment',504)
should result in a printed line as follows:
Fall 2020 Enrollment 504
print_float_line
----------------
Print out a line of text where the "text" parameter is left aligned
and takes up
a total of 45 characters and the "value" parameter is right aligned
and takes up
15 characters AND is displayed as a float value with TWO decimal
place. So, calling:
print_float_line('The value of e',2.718281828459045)
will print as follows:
The value of e 2.72
print_footer
------------
Print out a footer line that prints a single line of 60 "+"
characters followed
by a line that prints the "left", "middle" and "right" parameters
such that left
is aligned to the left, middle is centered, and right is aligned to
the right.
Each should take up 20 characters. In other words, if I call:
print_footer('1/1/2020','Summary Data','page 1 of 1')
your printed output should be:
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1/1/2020 Summary Data page 1 of 1
print_circle_area
-----------------
Finally, print out the surface are of a circle using your
print_float_line function AND
using your circle_area function from math_funcs. You will thus need
to import the
required function from math_funcs (remember that this is in the
"funcs" directory, so
you need to be sure to import it correctly). Call your
print_float_line function with the
results and appropriate description. For example, calling:
print_circle_area(5)
should result in a printed line as follows:
The area of a circle with radius of 5 78.54
'''
def print_header(text):
def print_integer_line(text,value):
def print_float_line(text,value):
def print_footer(left,middle,right):
def print_circle_area(radius):
if __name__ == "__main__":
import doctest
doctest.testmod()
Save the code in file
main.py
# define print_header function
def print_header(text):
'''
Print out a header line that prints the "text" parameter such that
it is centered and
takes up a total of 60 characters. It should then print a second
line that contains
60 "+" symbols. In other words, calling:
print_header('2021 Crop Report')
should print as follows:
2021 Crop Report
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
'''
print(text.center(60))
print('+' * 60)
# define print_integer_line function
def print_integer_line(text, value):
'''
print_integer_line
------------------
Print out a line of text where the "text" parameter is left aligned
and takes up
a total of 50 characters and the "value" parameter is right aligned
and takes up
10 characters. In other words, calling:
print_integer_line('Fall 2020 Enrollment',504)
should result in a printed line as follows:
Fall 2020 Enrollment 504
'''
print("{:50}".format(text) +"{:10d}".format(value));
# define print_float_line function
def print_float_line(text, value):
'''
print_float_line
----------------
Print out a line of text where the "text" parameter is left aligned
and takes up
a total of 45 characters and the "value" parameter is right aligned
and takes up
15 characters AND is displayed as a float value with TWO decimal
place. So, calling:
print_float_line('The value of e',2.718281828459045)
will print as follows:
The value of e 2.72
'''
print("{:45}".format(text) +"{:15.2f}".format(value));
# define print_footer function
def print_footer(leftText, midText, rightText):
'''
print_footer
------------
Print out a footer line that prints a single line of 60 "+"
characters followed
by a line that prints the "left", "middle" and "right" parameters
such that left
is aligned to the left, middle is centered, and right is aligned to
the right.
Each should take up 20 characters. In other words, if I call:
print_footer('1/1/2020','Summary Data','page 1 of 1')
your printed output should be:
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1/1/2020 Summary Data page 1 of 1
'''
print("{:20}".format(leftText) +"{:^20}".format(midText)
+"{:>20}".format(rightText));
# define print_circle_area function
def print_circle_area(radius):
'''
print_circle_area
-----------------
Finally, print out the surface are of a circle using your
print_float_line function AND
using your circle_area function from math_funcs. You will thus need
to import the
required function from math_funcs (remember that this is in the
"funcs" directory, so
you need to be sure to import it correctly). Call your
print_float_line function with the
results and appropriate description. For example, calling:
print_circle_area(5)
should result in a printed line as follows:
The area of a circle with radius of 5 78.54
'''
PI = 3.142
area = PI*radius*radius
print_float_line('The area of a circle with radius of', area)
def main():
print_header('2021 Crop Report')
print_integer_line('Fall 2020 Enrollment', 504)
print_float_line('The value of e',2.718281828459045)
print_footer('1/1/2020','Summary Data','page 1 of 1')
print_circle_area(5)
if __name__ == "__main__":
main()
import doctest
doctest.testmod(name ='print_header', verbose = True)
doctest.testmod(name ='print_integer_line', verbose = True)
doctest.testmod(name ='print_float_line', verbose = True)
doctest.testmod(name ='print_footer', verbose = True)
doctest.testmod(name ='print_circle_area', verbose =
True)
The image version of the code is also given as indentation is important in python
Here is the test output: