Using Python (Spyder),
Write a function convert_temperature that takes two arguments: a float parameter temp, the temperature to convert; and a str parameter conversion that determines which conversion to perform. Assume that conversion can take two possible values, 'celsius_to_fahrenheit' and 'fahrenheit_to_celsius', and make 'celsius_to_fahrenheit' the default value. Have your function print out a sentence explaining the conversion done, such as “0 °C converts to 32 °F”, and have the function return the converted value.
Your submission should include the function definition of followed by printing the result of three function calls: one that uses the default conversion type, and two that explicitly specify the conversion type, one for each possible conversion (Celsius to Fahrenheit and vice versa). You may (but are not required to) query the user for the input temperatures
PYTHON CODE:
def convert_temperature(temp, conversion = "celsius_to_fahrenheit"):
if(conversion == "celsius_to_fahrenheit"):
tempF = (temp * 9/5) + 32
print(str(temp) + " °C converts to " + str(tempF) + " °F")
return tempF
else:
tempC = (temp - 32) * 5/9
print(str(temp) + " °F converts to " + str(tempC) + " °C")
return tempC
# Testing above method
temp = 0
# Test 1: default conversion
convert_temperature(temp)
# Test 1: Celsius to Fahrenheit
convert_temperature(temp, "celsius_to_fahrenheit")
# Test 1: Fahrenheit to Celsius
convert_temperature(temp, "fahrenheit_to_celsius")
OUTPUT:
Get Answers For Free
Most questions answered within 1 hours.