In: Computer Science
PYTHON: Complete these functions in the module:
When your module is run on the command line as a standalone program, the _main() function will run.
Here's some code to get you started with your module. You have to add functionality to f2c, c2f, and _main(), and you have to write the function testing code. Complete any functions that contain a pass statement by adding your code.
# For pi import math # Use _main() for testing the module. You will see code at the bottom # of the script that will invoke _main() when the script is run as a # standalone script. def _main(): print("#" * 28,"\n", "Testing...1, 2, 3...testing!\n", "#" * 28, "\n", sep="") # code to test area() and circ() radius = 10 print("A circle with a radius of {} has a circumference of {:.2f} \ units and an area of {:.2f} square units.".format(radius, circ(radius), area(radius))) """ ---- ---- ---- ----- --- ------- ------ TEST WITH BOTH VALID AND INVALID INPUT! ---- ---- ---- ----- --- ------- ------ """ ######################################################## # TODO: write code to test the f2c() function ######################################################## ######################################################## # TODO: write code to test the c2f() function ######################################################## ######################################################## # TODO: write code to test the area() function ######################################################## ######################################################## # TODO: write code to test the circ() function ######################################################## # Calculate the area of a circle of a given radius def area(radius): return(radius * radius * math.pi) # Calculate the circumference of a circle of a given radius def circ(radius): return(radius * 2 * math.pi) ################################################ # TODO: Complete the functions below for credit ################################################ """ Triple quotes can be used to create multi-line comments For temperature functions it's important to allow only valid temperatures. Your functions have to check that the user gives the function valid temperatures. The lowest possible temperature, by international agreement, is absolute zero (zero on the Kelvin scale). At 0 K (–273.15 °C; –459.67 °F), nearly all molecular motion ceases. Your script should check that the input temperatures is not less than –273.15°C or –459.67°F. The maximum possible temperature is 1.416785(71)×10**32 kelvins (142 quintillion kelvins) --- there is no existing scientific theory for the behavior of matter at these energies. """ ############################### # TODO: COMPLETE THIS FUNCTION ############################### def f2c(temp): """ Converts Fahrenheit tempature to Celcius USAGE: print(f2c(f_temp)) """ pass ############################### # TODO: COMPLETE THIS FUNCTION ############################### def c2f(temp): """ Converts Celcius to Fahrenheit USAGE: print(c2f(c_temp)) """ pass # testing code # When run on the command line, this code will run # but when the module is imported, it won't run. # In _main(), test all of our modules' function. if __name__ == '__main__': _main()
When run in testing mode your module should generate output for all of the functions defined in the module:
############################ Testing...1, 2, 3...testing! ############################ A circle with a radius of 10 has a circumference of 62.83 units and an area of 314.16 square units. etc. etc.
The incomplete functions f2c and c2f return False, which is the default return value of Python functions.
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. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks
import math
# Use _main() for testing the module. You will see code at the bottom
# of the script that will invoke _main() when the script is run as a
# standalone script.
def _main():
print("#" * 28, "\n", "Testing...1, 2, 3...testing!\n", "#" * 28, "\n", sep="")
# code to test area() and circ()
radius = 10
print("A circle with a radius of {} has a circumference of {:.2f} \
units and an area of {:.2f} square units.".format(radius, circ(radius), area(radius)))
"""
---- ---- ---- ----- --- ------- ------
TEST WITH BOTH VALID AND INVALID INPUT!
---- ---- ---- ----- --- ------- ------
"""
########################################################
# DONE: write code to test the f2c() function
########################################################
print("\n### Testing f2c() with valid inputs ###")
f = 0
print("{:.2f} F is {:.2f} C".format(f, f2c(f)))
f = -200
print("{:.2f} F is {:.2f} C".format(f, f2c(f)))
f = 125
print("{:.2f} F is {:.2f} C".format(f, f2c(f)))
print("\n### Testing f2c() with invalid input ###")
f = -460
print("f2c({:.2f}): {}".format(f, f2c(f)))
########################################################
# DONE: write code to test the c2f() function
########################################################
print("\n### Testing c2f() with valid inputs ###")
c = 0
print("{:.2f} C is {:.2f} F".format(c, c2f(c)))
c = -200
print("{:.2f} C is {:.2f} F".format(c, c2f(c)))
c = 125
print("{:.2f} C is {:.2f} F".format(c, c2f(c)))
print("\n### Testing c2f() with invalid input ###")
c = -300
print("c2f({:.2f}): {}".format(c, c2f(c)))
########################################################
# DONE: write code to test the area() function
########################################################
print("\n### Testing area() ###")
radius = 15
print("area of circle with radius: {} is {:.2f}".format(radius, area(radius)))
radius = 100
print("area of circle with radius: {} is {:.2f}".format(radius, area(radius)))
########################################################
# DONE: write code to test the circ() function
########################################################
print("\n### Testing circ() ###")
radius = 15
print("circumference of circle with radius: {} is {:.2f}".format(radius, circ(radius)))
radius = 100
print("circumference of circle with radius: {} is {:.2f}".format(radius, circ(radius)))
# Calculate the area of a circle of a given radius
def area(radius):
return (radius * radius * math.pi)
# Calculate the circumference of a circle of a given radius
def circ(radius):
return (radius * 2 * math.pi)
################################################
# DONE: Complete the functions below for credit
################################################
""" Triple quotes can be used to create multi-line comments
For temperature functions it's important to allow only valid temperatures.
Your functions have to check that the user gives the function valid
temperatures.
The lowest possible temperature, by international agreement,
is absolute zero (zero on the Kelvin scale). At 0 K (–273.15 °C; –459.67 °F),
nearly all molecular motion ceases. Your script should
check that the input temperatures is not less than –273.15°C or –459.67°F.
The maximum possible temperature is 1.416785(71)×10**32 kelvins
(142 quintillion kelvins) --- there is no existing scientific theory
for the behavior of matter at these energies.
"""
###############################
# DONE: COMPLETE THIS FUNCTION
###############################
def f2c(temp):
"""
Converts Fahrenheit tempature to Celcius
USAGE: print(f2c(f_temp))
"""
if temp < -459.67: # if temp is below -459.67F, returning False
return False
c = (temp - 32) * (5 / 9) # otherwise converting temp to celsius
return c
###############################
# DONE: COMPLETE THIS FUNCTION
###############################
def c2f(temp):
"""
Converts Celcius to Fahrenheit
USAGE: print(c2f(c_temp))
"""
if temp < -273.15: # if temp is below -273.15C, returning False
return False
f = (temp * 9 / 5) + 32 # otherwise converting temp to fahrenheit
return f
# testing code
# When run on the command line, this code will run
# but when the module is imported, it won't run.
# In _main(), test all of our modules' function.
if __name__ == '__main__':
_main()
/*OUTPUT*/
############################
Testing...1, 2, 3...testing!
############################
A circle with a radius of 10 has a circumference of 62.83 units and an area of 314.16 square units.
### Testing f2c() with valid inputs ###
0.00 F is -17.78 C
-200.00 F is -128.89 C
125.00 F is 51.67 C
### Testing f2c() with invalid input ###
f2c(-460.00): False
### Testing c2f() with valid inputs ###
0.00 C is 32.00 F
-200.00 C is -328.00 F
125.00 C is 257.00 F
### Testing c2f() with invalid input ###
c2f(-300.00): False
### Testing area() ###
area of circle with radius: 15 is 706.86
area of circle with radius: 100 is 31415.93
### Testing circ() ###
circumference of circle with radius: 15 is 94.25
circumference of circle with radius: 100 is 628.32