Knowee
Questions
Features
Study Tools

1.1.2. Write a Python program to convert temperatures to and from Celsius, Fahrenheit. 25:45 Write a Python program to convert temperatures to and from Celsius, Fahrenheit. Sample Test Cases Test case 1 Enter·the·temperature·in·celsius·or·fahrenheit:·215c The·temperature·in·Fahrenheit·is·419·degrees⏎ Test case 2 Enter·the·temperature·in·celsius·or·fahrenheit:·105F The·temperature·in·Celsius·is·41·degrees⏎

Question

1.1.2. Write a Python program to convert temperatures to and from Celsius, Fahrenheit. 25:45 Write a Python program to convert temperatures to and from Celsius, Fahrenheit.

Sample Test Cases Test case 1 Enter·the·temperature·in·celsius·or·fahrenheit:·215c The·temperature·in·Fahrenheit·is·419·degrees⏎ Test case 2 Enter·the·temperature·in·celsius·or·fahrenheit:·105F The·temperature·in·Celsius·is·41·degrees⏎

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

Sure, here is a Python program that can convert temperatures to and from Celsius and Fahrenheit:

def convert_temperature(temp):
    if temp[-1] in ['C', 'c']:
        celsius = int(temp[:-1])
        fahrenheit = (celsius * 9/5) + 32
        return f"The temperature in Fahrenheit is {fahrenheit} degrees"
    elif temp[-1] in ['F', 'f']:
        fahrenheit = int(temp[:-1])
        celsius = (fahrenheit - 32) * 5/9
        return f"The temperature in Celsius is {celsius} degrees"
    else:
        return "Invalid input. Please enter temperature followed by 'C' for Celsius or 'F' for Fahrenheit."

# Test cases
print(convert_temperature('215c'))  # Expected output: The temperature in Fahrenheit is 419.0 degrees
print(convert_temperature('105F'))  # Expected output: The temperature in Celsius is 40.55555555555556 degrees

This program defines a function convert_temperature(temp) that takes a string temp as input. The string should represent a temperature, followed by either 'C' or 'F' to indicate Celsius or Fahrenheit, respectively.

The function first checks the last character of the string to determine whether the temperature is in Celsius or Fahrenheit. If the last character is 'C' or 'c', it converts the temperature to Fahrenheit using the formula (celsius * 9/5) + 32. If the last character is 'F' or 'f', it converts the temperature to Celsius using the formula (fahrenheit - 32) * 5/9.

If the last character is neither 'C' nor 'F', the function returns an error message.

Finally, the program tests the function with two test cases:

This problem has been solved

Similar Questions

Write code that allow the user to enter the temperature in degrees Celsius. The program should convert it and print it in Fahrenheit degrees using the following formula: Fahrenheit =  (Celsius * 9/5) + 32 For example, if the user enters 0, then the program will print "Temperature in Fahrenheit: 32.0"

Given the temperature of the day as Celsius as input. Do convert Celsius into Fahrenheit using the formulae: Fahrenheit = ((celcius*9) /5)+32Input Format:Accept a floating point(double) value for Celsius.Output Format:Print the Fahrenheit value with respect to two decimal places(Example: 23.52F)Constraints:1<=N<=10^15Sample Input 1:32.4Sample Output 1:90.32FSample Input 2:20.0

Create a Temprature class. Make two methods :1. convertFahrenheit - It will take celsius and will print it into Fahrenheit.2. convertCelsius - It will take Fahrenheit and will convert it into Celsius.

Question 4An analyst at a weather station is analyzing data to understand how cloud cover affects temperature in their area. Currently, they have data on temperature in Celsius, but they want to convert this data to Fahrenheit. Which formula should they use to convert a Celsius value in cell G5 to Fahrenheit in another cell?1 point=CONVERT(“F”, “C”, G5)=CONVERT(G5, “F”, “C”)=CONVERT(“C”, “F”, G5)=CONVERT(G5, “C”, “F”)

Which is the correct formula for converting degrees Celsius to degree Fahrenheit

1/2

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.