# Print air_temperature with 1 decimal point followed by c. sample output from given program:

975 views

## Problem :

I learning Python with the help of free online Python tutorial which wants me to write following Program:

Create a temperature converter which will convert Fahrenheit values to Celsius and vice-versa using the following two formulas which relate the temperature f in Fahrenheit to the temperature c in Celsius:

`    f = c *  9/5 + 32`
`    c = (f -32)* 5/9 `

The input will be a string consisting of a floating-point number followed immediately by the letter F or C, such as "13.2C". I need to convert to the other temperature scale and print the converted value in the same format. For example, if the input is "8F" then the output should be (approximately) "-13.333C", and if the input is "12.5C" then the output should be "54.5F".

My answers are always slightly wrong. Is there any problem in my code? My code is as follows:

`def celsiusConvert(farenheit):`
`   return (farenheit - 32)*(5/9)`
`def farenheitConvert(celsius):`
`   return ((celsius*(9/5)) + 32)`
`inputString = input()`
`inputDigit = float(inputString[0:-2])`
`if inputString[-1] == 'C':`
`   celsius = inputDigit`
`   print(farenheitConvert(celsius),'F',sep ='')`
`if inputString[-1] == 'F':`
`   farenheit = inputDigit`
`   print(celsiusConvert(farenheit),'C', sep='')`

## Solution :

You are cutting the last 2 characters not just the last one:

`inputDigit = float(inputString[0:-2])`

should be:

`inputDigit = float(inputString[0:-1])`

This why you are facing accuracy problem as follows:

`>>> celsiusConvert(2.4)`
`-16.444444444444446`
`>>> celsiusConvert(2.49)`
`-16.394444444444446`

Note : slicing counts from the end slicing to :-2 will cut of both the unit and the last digit as follows:

`>>> '2.49F'[:-2]`
`'2.4'`
`>>> '2.49F'[:-1]`
`'2.49'`

## Related questions

123 views
Problem : I am unable to understand what am I missing here: Sort my_short_names in reverse alphabetic order. The sample output from the given program as below : ['Tody', 'Samy', 'Joey', 'Jany', 'Anny'] Below is my code for your reference : my_short_names = ['Jany', 'Samy', 'Anny', 'Joey', 'Tody'] my_short_names.sort() print(my_short_names)
8 views
1 view
Problem: I am reading a text file with floating-point numbers, all with either 1 or 2 decimal points. I am using float() to convert a line into a float and raising a ValueError if that fails. I am storing all floats in a list. When printing it out, I'd like to print it out as a 2 decimal place floating-point.
3 views
Problem: Please solve it &hellip; Because I am unable to find out the solution... import sys for line in sys.stdin: print(line, end="")
587 views
Problem : When I try to use a print statement in Python, it gives me below error: SyntaxError: Missing parentheses in call to 'print'
3 views
Problem: Anyone here who know nested loops to print a rectangle. sample output for given program? If yes, then please answer me.
6 views
7 views
48 views
Problem: I just started Bachelors in Software Engineering. Programming is the main subject of Software Engineering and I have no prior knowledge of programming. My programming teacher gave me an assignment in which I have to write a program in C++ whose output should be as ... a hint: Use nested loop. If someone knows how to do this kindly explain and provide me with the example program. Thanks.