我在第5行得到TypeError: unsupported operands (str & int)
。我已经查找了如何修复它但是没有任何对我有用(它是一项学校作业,我必须使用if
,elif
,else
形式)。我也似乎无法在不声明x和y的情况下定义变量。
我甚至无法看到if
之后的打印陈述是如何受到影响的,因为我无法通过第5行错误。有什么问题?
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.")
好的家伙所以我发现了我的问题,这是新的代码。 str / int错误来自于不将str函数放在温度变量之前。对于任何想知道的人,我把它留在这里。抱歉没有在开头提供完整的代码,我不想让它烦人。
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.")
答案 0 :(得分:0)
在输入之前将温度声明为int。
Temperature=0
尝试使用raw_input而不是输入。
Temperature=int (raw_input ("What's the temperature outside?"))
或者创建一个整数类型变量,然后在需要的地方进行分配。
n=0
n=raw_input ("What's the temperature outside?")
Temperature=n