-1

Write the code to convert Temperature:

#TempConvert.py
val = input("Input Temperature(eg. 32C): ")
if val[-1] in ['C','c']:
    f = 1.8 * float(val[0:-1]) + 32
    print("Converted Temperture : %.2fF"%f)
elif val[-1] in ['F','f']:
    c = (float(val[0:-1]) - 32) / 1.8
    print("Converted Temperture: %.2fC"%c)
else:
    print("Input Error")

When runing the code in Python2.7, get an error :

    enter code ============= RESTART: D:\workshop_for_Python\TempConvert -2.py =============
Input Temperture(eg. 32C): 33C

Traceback (most recent call last):
  File "D:\workshop_for_Python\TempConvert -2.py", line 2, in <module>
    val = input("Input Temperture(eg. 32C): ")
  File "<string>", line 1
    33C
      ^
SyntaxError: unexpected EOF while parsinghere

Any idea whats the issue?Thanks a lot~

Tommy
  • 1
  • 1
  • Possible duplicate of [input() error - NameError: name '...' is not defined](https://stackoverflow.com/questions/21122540/input-error-nameerror-name-is-not-defined) – anthony sottile Aug 25 '17 at 02:46

1 Answers1

2

The source of error is use of input() for taking input as it only allows reading of integer value. So only modification in code will be use of raw_input() :

#TempConvert.py
val = raw_input("Input Temperature(eg. 32C): ")
if val[-1] in ['C','c']:
    f = 1.8 * float(val[0:-1]) + 32
    print("Converted Temperture : %.2fF"%f)
elif val[-1] in ['F','f']:
    c = (float(val[0:-1]) - 32) / 1.8
    print("Converted Temperture: %.2fC"%c)
else:
    print("Input Error")