【问题标题】: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 的结果是什么?为什么?

标签: python machine-learning streamlit


【解决方案1】:

根据您的示例进行编辑:

import streamlit as st

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

【讨论】:

    猜你喜欢
    • 2013-12-24
    • 2011-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-18
    • 2022-07-27
    • 2016-04-15
    • 2012-11-29
    相关资源
    最近更新 更多