python - Python:字符串连接类型错误 - 一元 + 的错误操作数类型:'str'
问题描述
我正在编写一个简单的函数来确定风向/风速。使用 Darksky Web API。试图打印一个包含两个变量的句子,速度和方向。
我尝试过将风速/方向变量设置为 ints、float 或 strs。
weather = forecast('api_key',lat, -long)
windbearing = weather.windBearing
windspeed = float(weather.windSpeed)
def windcompass(windbearing):
val = int((windbearing/22.5)+.5)
argument = ["N","NNE","NE","ENE","E","ESE", "SE", "SSE","S","SSW","SW","WSW","W","WNW","NW","NNW"]
return argument[(val % 16)]
direction = windcompass(windbearing)
print('The wind is blowing ', + windspeed, + 'at ', + direction, + 'MPH')
我收到此错误:
TypeError:一元+的错误操作数类型:'str'
解决方案
不要在打印功能中使用逗号:
print( str1 + str2 + ...)
您可能想先将ints
/转换floats
为str
:
print(str(float_value) + str(int_value) + ...)
示例(您的代码):
print('The wind is blowing ' + str(windspeed) + ' at ' + str(direction) + ' MPH')
推荐阅读
- javascript - 按对象的键/值的打字稿动态功能
- python - 对每次迭代中调用两次的同一方法执行多处理
- reactjs - TypeError:无法读取未定义的属性“错误”
- docker - 如何在我的 docker 镜像中安装 openmodelica?
- bash - 将两个数组组合成一个具有两个属性的对象数组
- javascript - 如何将我的对象从仅包含内部数组的一个对象更改为一组对象?
- gulp - MIXIN_WITHOUT_BODY - 哈巴狗混合
- r - 如何合并相似但不相同的行?
- android - java.lang.IllegalArgumentException:URL 查询字符串 对于动态查询参数,请使用 @Query。对于方法 OpenWeatherApi.currentWeatherDetails
- flutter - 具有通用回调的冻结类