-1

I get TypeError: unsupported operands (str & int) at about line 5. I've looked up how to fix it but nothing has worked for me (it's a school assignment and I must use the if, elif, else form). I also couldn't seem to define the variable without declaring them x and y.

I can't even see how my print statements after the if are affected because I can't get past the line 5 error. What is the problem there?

Temperature=int(input("What's the temperature outside?"))
Type=input("Is this Fahrenheit or Celsius?")
  print("So it's " + str(Temperature) + " degrees " + Type + " outside.")
Conversion=input("Do you want to convert this to Fahrenheit or Celsius. (Type F or C.)")
if(Conversion.lower()=="c"):
  print(Temperature+ " degrees Celsius is " +str((Temperature * 1.8)+32) + " degrees Fahrenheit.")
elif(Conversion.lower()=="f"):
  print(Temperature+ " degrees Celsius is " +str((Temperature * 1.8)+32) + " degrees Fahrenheit.")
else:
  print("You need to type either fahrenheit or celsius when it asks you. Re-run the program.")

ok guys so I found my problem, here is the new code. The str/int error comes from not putting the str function in front of the temperature variable. I leave it up here for anybody wondering. And sorry for not providing the full code at the beginning, I didnt want it to be annoying.

Temperature=int(input("What's the temperature outside?"))
Type=input("Is this Fahrenheit or Celsius?")
  print("So it's " + str(Temperature) + " degrees " + Type + " outside.")
Conversion=input("Do you want to convert this to Fahrenheit or Celsius. (Type F or C.)")
if(Conversion.lower()=="f"):
  print(str(Temperature) + " degrees Celsius is " + str((Temperature* 1.8)+32) + " degrees Fahrenheit.")
elif(Conversion.lower()=="c"):
  print(str(Temperature)+ " degrees Fahrenheit is " +str((Temperature -32)*5/9) + " degrees Celsius")
else:
  print("You need to type either fahrenheit or celsius when it asks you. Re-run the program.")
Shawn
  • 1
  • 1

1 Answers1

0

Declare Temperature as an int before input.

Temperature=0

Try using raw_input instead of input.

Temperature=int (raw_input ("What's the temperature outside?"))

Or create an integer type variable and than assigne it where you need it.

n=0
n=raw_input ("What's the temperature outside?")
Temperature=n
  • My teacher wanted us to do using the if, elif, else function while also messing with variables. A few of my classmates used the def function they found online but, she wanted us to show our knowledge with this first. I'm actually writing this from class right now. Thanks anyway. – Shawn Jan 02 '18 at 19:07