In: Computer Science
(PYTHON) A beard-second is a unit of length inspired by the light-year and is defined as the length the average beard grows in one second, or precisely 5 nanometers. The beard-second is used for extremely short distances such as measurements within an integrated circuit. Your manufacturing equipment is very accurate but it uses inches as a unit of measurement, so you need a conversion table between beard-seconds and inches to complete a special order.
The following information should be helpful: 1 beard second = 5 nanometers 1 nanometer = 3.937e-8 inches
Write a program named beard.py that includes two functions main() and inches()which are used to produce a conversion table between beard seconds and inches for a series of values specified by the user.
The main() function: • Displays a description of the program’s function • Prompts the user for the starting and ending beard second value to be converted to inches • Displays a table header (see examples) • Using a loop, calls the inches() function repeatedly to calculate the converted values • The beard second values should increment by 250
The inches() function: • Accepts one (1) value in beard seconds as an argument • Performs the conversion calculation from beard seconds to inches • Displays the table row including the beard seconds and inches values (to 8 decimal places) • Beard second values should have a thousands comma separator • Does not return any values
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
Note: Please maintain proper code spacing (indentation), just copy the code part and paste it in your compiler/IDE directly, no modifications required.
#CODE
#constants for measurements in different units
NANOMETER=3.937e-8 #nanometer to inches
BEARD_SECOND_TO_INCHES=NANOMETER*5 #beard second to nanometers
to inches
#required method to convert beard seconds to inches
def inches(beard_sec):
#converting beard_sec to inches
inc=beard_sec*BEARD_SECOND_TO_INCHES
#creating a formatted string containing inc
separated by comma for every
#1000's places
sec='{:,}'.format(beard_sec)
#displaying sec with a field width of 10
spaces and inc with a precision of 8
#digits after the decimal point
print('{:10s}
{:.8f}'.format(sec,inc))
#main method
def main():
#displaying description
print('This program converts beard
seconds to inches')
#reading start and end values for beard
seconds
start=int(input("Enter starting
beard second value: "))
end = int(input("Enter ending beard
second value: "))
#displaying heading
print('\n{:10s}
{:.8s}'.format("Beard Sec",
"Inches"))
#looping as long as start <= end
while start<=end:
#displaying beard
seconds and inches for start
inches(start)
#incrementing start
by 250
start+=250
#calling main()
main()
#OUTPUT