'int' object has no attribute 'strip' error message

*爱你&永不变心* 提交于 2021-01-29 02:12:53

问题


This is my program and what means this error?

def menuAdiciona():
    nome = input("Digite o nome de quem fez o cafe: ")
    nota = int(input("Que nota recebeu: "))

    if len(nome.strip()) == 0:
        menuAdiciona()

    if len(nota.strip()) == 0:
        menuAdiciona()

    if nota < 0:
        nota = 0

AttributeError: 'int' object has no attribute 'strip'.


回答1:


You are trying to call strip() on the nota value, which is an integer. Don't do that.




回答2:


If you have a case for which you don't know whether the incoming value is a string or something else, you can deal with this using a try...except:

try:
    r = myVariableOfUnknownType.strip())
except AttributeError:
    # data is not a string, cannot strip
    r = myVariableOfUnknownType



回答3:


integer has no strip attribute... so you can make it like this...

if len(str(nome).strip()) == 0:
    menuAdiciona()

if len(str(nota).strip()) == 0:
    menuAdiciona()



回答4:


strip is only available for strings

if you desperately need to strip numbers try this:

str(nome).strip()

str(nota).strip()

so it turns out to be:

def menuAdiciona():
nome = input("Digite o nome de quem fez o cafe: ")
nota = int(input("Que nota recebeu: "))

if len(str(nome).strip()) == 0:
    menuAdiciona()

if len(str(nota).strip()) == 0:
    menuAdiciona()

if nota < 0:
    nota = 0


来源:https://stackoverflow.com/questions/17191741/int-object-has-no-attribute-strip-error-message

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!