Question

In: Computer Science

PYTHON: Complete these functions in the module: f2c, a function to convert Fahrenheit temperatures to Celcius....

PYTHON: Complete these functions in the module:

  1. f2c, a function to convert Fahrenheit temperatures to Celcius.
  2. c2f, a function to convert Celcius termperatures to Fahrenheit.
  3. Write f2c() and c2f() as functions that return a true value or False. In other words, these functions do not print — they return. A "true value", in this case, would be a number that corresponds to a temperature.
  4. If the user enters an invalid temperature, the function should return False.
  5. Create _main(), a function for testing the all of the functions in your module (f2c, c2f, circ, and area).
    • When your module is run on the command line as a standalone program, the _main() function will run.

    • When your module is required by a program, the _main() function will not 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.

Solutions

Expert Solution

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

Related Solutions

Develop a routine to convert Celcius to Fahrenheit. Have a form pass the values and a...
Develop a routine to convert Celcius to Fahrenheit. Have a form pass the values and a parm indicating what time of conversion is taking place. Use a function (if possible). Return the correct value. Here is the formula: C = (F - 32) * 5/9 hw8.html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Untitled Document</title></head> <body> <p>Convert Temp</p> <form id="form1" name="form1" method="get" action="hw8.php"> <table width="300" border="1"> <tr> <td width="122">Enter Temp </td>...
1. Develop a module named temperatures.py with the following functions. Function toCelsius is passed a Fahrenheit...
1. Develop a module named temperatures.py with the following functions. Function toCelsius is passed a Fahrenheit temperature and returns the Celsius equivalent. 1 degree Fahrenheit = 5/9 * (Fahreheit-32) degrees Celsius. Function toFahrenheit is passed a Celsius temperature and returns the Fahrenheit equivalent. 1 degree Celsius = 1.8*Celsisus + 32 degrees Fahrenheit. Function toKPH is passed a speed in miles per hour and returns the kilometers per hour equivalent. 1 kph = mph * 1.609344 Function toMPH is passed a...
Python 3 Forming Functions Define and complete the functions described below. * function name: say_hi *...
Python 3 Forming Functions Define and complete the functions described below. * function name: say_hi * parameters: none * returns: N/A * operation: just say "hi" when called. * expected output: >>> say_hi() hi * function name: personal_hi * parameters: name (string) * returns: N/A * operation: Similar to say_hi, but you should include the name argument in the greeting. * expected output: >>> personal_hi("Samantha") Hi, Samantha * function name: introduce * parameters: name1 (string) name2 (string) * returns: N/A...
Python 3 Functions that give answers Define and complete the functions described below. * function name:...
Python 3 Functions that give answers Define and complete the functions described below. * function name: get_name * parameters: none * returns: string * operation: Here, I just want you to return YOUR name. * expected output: JUST RETURNS THE NAME...TO VIEW IT YOU CAN PRINT IT AS BELOW >>> print(get_name()) John * function name: get_full_name * parameters: fname (string) lname (string) first_last (boolean) * returns: string * operation: Return (again, NOT print) the full name based on the first...
PYTHON: Write a script that imports the functions in the module below and uses all of...
PYTHON: Write a script that imports the functions in the module below and uses all of the functions. import math def _main():     print("#" * 28, "\n", "Testing...1, 2, 3...testing!\n", "#" * 28, "\n", sep="")     f = -200     print("{:.2f} F is {:.2f} C".format(f, f2c(f)))     f = 125     print("{:.2f} F is {:.2f} C".format(f, f2c(f)))     c = 0     print("{:.2f} C is {:.2f} F".format(c, c2f(c)))     c = -200     print("{:.2f} C is {:.2f} F".format(c, c2f(c))) def f2c(temp):     #Converts Fahrenheit tempature to Celcius     if temp <...
Create and submit a Python program (in a module) that contains a main function that prints...
Create and submit a Python program (in a module) that contains a main function that prints 17 lines of the following text containing your name: Welcome to third week of classes at College, <your name here>! can someone please show me how to do this step by step? I'm just confused and keep getting errors in idle.
Complete the code so that it can convert the date to day of week using python,...
Complete the code so that it can convert the date to day of week using python, the code should pass the doctest def convert_datetime_to_dayofweek(datetime_string): """ This function takes date in the format MON DAY YEAR HH:MM(PM/AM) and returns the day of the week Assume input string is UTC    >>> convert_datetime_to_dayofweek('Jun 1 2005 1:33PM') 'Wednesday' >>> convert_datetime_to_dayofweek('Oct 25 2012 2:17AM') 'Thursday' """ # code goes here
At what temperatures values are the following scales the same? (a) The Fahrenheit and the Celsius   ...
At what temperatures values are the following scales the same? (a) The Fahrenheit and the Celsius    (b) The Celsius and the Kelvin     (c) The Fahrenheit and the Kelvin  
Use Python for this quetions: Write a python functions that use Dictionary to: 1) function name...
Use Python for this quetions: Write a python functions that use Dictionary to: 1) function name it addToDictionary(s,r) that take a string and add it to a dictionary if the string exist increment its frequenc 2) function named freq(s,r) that take a string and a record if the string not exist in the dictinary it return 0 if it exist it should return its frequancy.
Programming language Python It have to be in Functions with a main function Samuel is a...
Programming language Python It have to be in Functions with a main function Samuel is a math teacher at Hogwarts School of Witchcraft and Wizardry. He loves to give his students multiplication exercises. However, he doesn’t care about the actual operation result but the unit sum of its digits. At Hogwarts School of Witchcraft and Wizardry, they define the unit sum (US) of N as the unit that it is left after doing the sum of all the digits of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT