Streamlit:TypeError:+的不支持的操作数类型:'int'和'str'[重复]

Posted

技术标签:

【中文标题】Streamlit:TypeError:+的不支持的操作数类型:\'int\'和\'str\'[重复]【英文标题】:Streamlit: TypeError: unsupported operand type(s) for +: 'int' and 'str' [duplicate]Streamlit:TypeError:+的不支持的操作数类型:'int'和'str'[重复] 【发布时间】:2020-05-25 05:04:09 【问题描述】:

我的 streamlit 应用程序中出现下一个错误:

我的错误是:

TypeError: +: 'int' 和 'str' 的操作数类型不受支持

我的代码是:

import streamlit as st

if st.button("SUBMIT"):

    calculation = 23*2

    string = "Hello"

    result= (calculation + str(string))

    st.success(result)

【问题讨论】:

calculation 是一个整数。在 python 中,你不能直接将整数与字符串相加。使用str(calculation) 代替 再次阅读错误信息。您尝试添加一个整数和一个字符串。 calculation46,这是一个整数。 str(string)) 是一个字符串(我不确定你为什么要将字符串 "Hello" 再次转换为字符串)。如果您问“为什么没有自动转换”,请回答以下问题。您希望"1" + 2 的结果是什么?为什么? 【参考方案1】:

根据您的示例进行编辑:

import streamlit as st

if st.button("SUBMIT"):
    calculation = 23*2
    string = "Hello"
    result = (str(calculation) + string)
    st.success(result)

【讨论】:

以上是关于Streamlit:TypeError:+的不支持的操作数类型:'int'和'str'[重复]的主要内容,如果未能解决你的问题,请参考以下文章