• Register
0 votes

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='')
6 5 3
6,930 points

Please log in or register to answer this question.

1 Answer

0 votes

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)
>>> celsiusConvert(2.49)

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.49F'[:-1]
9 7 4
38,600 points

Related questions

0 votes
1 answer 103 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)
asked Jan 8, 2020 alecxe 7.5k points
0 votes
2 answers 419 views
Problem : When I try to use a print statement in Python, it gives me below error: SyntaxError: Missing parentheses in call to 'print'
asked Oct 22, 2019 peterlaw 6.9k points
0 votes
1 answer 93 views
Problem: I have freshly migrated my project from the old python2 to the new python3 and it has hundreds of the ForeignKey relations. I have very basic knowledge related to python. Using a solution to go line by line and add on_delete argument is not the possible. What ... it does not look like catching it somehow. Looking for a Python expert who can try to fix my above Python error very quickly.
asked Jul 15, 2020 Raphael Pacheco 4.9k points
0 votes
1 answer 22 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.
asked Nov 29, 2020 Code Learner 5.5k points
1 vote
3 answers 2.6K views
Problem : I am very new to Python. Now I am trying to import my function from the another file in a same directory. I am able to do it sometimes with the use of .mymodule import myfunction but most of the times I face below error: Importerror: attempted relative ... . It seems completely random to me. I am looking for someone who is expert in Python and who can help me in fixing above error.
asked Apr 20, 2020 stewart 4k points
1 vote
1 answer 111 views
Problem : I am trying to find solution for following error replace any space ' ' by '_' in 2-character string passcode. sample output for the given program:
asked Nov 11, 2019 peterlaw 6.9k points
0 votes
1 answer 19 views
Problem : I just installed Python 3.5 and upon running the following code pip install mysql-python I am getting an error, which says: error: Microsoft Visual C++ 14.0 is required (Unable to find vcvarsall.bat) I have used the following lines of code to my PATH C:\ ... (x86)\Microsoft Visual Studio 12.0\VC; C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC My PC has 64bit window 7 setup.
asked Oct 8, 2020 sikandar 2.4k points
0 votes
1 answer 5 views
Problem: I am having trouble rounding a GPA double to 2 decimal places. (ex of a GPA needed to be rounded: 3.67924) I am currently using ceil to round up, but it currently outputs it as a whole number (368)
asked Dec 15, 2020 Mashhoodch 5.6k points
0 votes
1 answer 16 views