HoujuuNue
HoujuuNue

Reputation: 21

TypeError:unsupported operand type[s] for -: 'list' and 'int'

What have I done wrong? I'm just starting to learn python, so I do not understand much, help please. Task: Convert degrees Fahrenheit to degrees Celsius and vice versa. The integer degrees of Fahrenheit and Celsius are given in different lines. Print the calculated translation values on different lines.

Input:95_F
      73_C
a=input().split('_F')
b=input().split('_C')
a1=(5/9*(a-32))
a2=(9/5*b+32)
print (a1)
print (a2)
Traceback (most recent call last):
  File "jailed_code", line 3, in <module>
    a1=(5/9*(a-32))
TypeError: unsupported operand type(s) for -: 'list' and 'int'

Upvotes: 2

Views: 1946

Answers (2)

Pittman
Pittman

Reputation: 54

split return list (element type equal string)

a=input().split('_F')
b=input().split('_C')

a=int(a[0])
b=int(b[0])

a1=(5/9*(a-32))
a2=(9/5*b+32)
print (a1)
print (a2)

Upvotes: 0

Devesh Kumar Singh
Devesh Kumar Singh

Reputation: 20500

input().split() gives you a list, and you are trying to subtract it with an int, hence the error TypeError: unsupported operand type(s) for -: 'list' and 'int'

To fix it, you want to get the first element of the list after splitting the string via split, and convert that string to an integer via int(var)

#Get the first element of the list after splitting the string, and convert that string to an integer
a=int(input().split('_F')[0])
b=int(input().split('_C')[0])

#Do the conversion
a1=(5/9*(a-32))
a2=(9/5*b+32)

#Print the temperatures
print(a1)
print(a2)

The output will be

35.0
163.4

Upvotes: 2

Related Questions