问题
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