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.")