Jump to content

Applied Programming/Functions/Python3

From Wikiversity

functions.py

[edit | edit source]
"""This program converts a Fahrenheit temperature to Celsius.

Input:
    Fahrenheit temperature

Output:
    Fahrenheit temperature
    Celsius temperature

Example:
    Enter Fahrenheit temperature:
    100
    100.0° Fahrenheit is 37.8° Celsius

References:
    * http://sphinxcontrib-napoleon.readthedocs.io/en/latest/example_google.html
    * http://www.mathsisfun.com/temperature-conversion.html

"""


def get_fahrenheit():
    """Gets Fahrenheit temperature.

    Args: None

    Returns:
        float: Fahrenheit temperature

    """
    print("Enter Fahrenheit temperature:")
    fahrenheit = float(input())
    return fahrenheit


def fahrenheit_to_celsius(fahrenheit):
    """Converts Fahrenheit temperature to Celsius.

    Args:
        fahrenheit (float): Fahrenheit temperature to be converted

    Returns:
        float: Celsius temperature

    """
    TEMPERATURE_DIFFERENCE = 32
    TEMPERATURE_RATIO = 5 / 9
    celsius = (fahrenheit - TEMPERATURE_DIFFERENCE) * TEMPERATURE_RATIO
    return celsius


def display_results(fahrenheit, celsius):
    """Displays Fahrenheit and Celsius temperatures.

    Args:
        fahrenheit (float): Fahrenheit temperature
        celsuis (float): Celsius temperature

    Returns: None

    """
    print(f'{fahrenheit:.1f}° Fahrenheit is {celsius:.1f}° Celsius')
    

def main():
    """Runs the main program logic."""

    fahrenheit = get_fahrenheit()
    celsius = fahrenheit_to_celsius(fahrenheit)
    display_results(fahrenheit, celsius)


main()

Try It

[edit | edit source]

Copy and paste the code above into one of the following free online development environments or use your own Python3 compiler / interpreter / IDE.

See Also

[edit | edit source]