新人求教
class Dog:def __init__(self,name):
self.name=name
self.__age=None
def set_age(self,age):
if not isinstance(age,int):
print("请输入的年龄必须是数字!")
return False
if age <=0:
print("年龄必须大于零!")
return False
self.__age=age
def play(self):
print("今年"+self.__age)
dog=Dog("旺财")
dog.set_age('hello')
dog.set_age(-20)
dog.set_age(3)
dog.play()
为啥他运行到self.__age=age说我不是int行(就是set.age(3)) 咋不把错误信息贴出来呢 wp231957 发表于 2021-5-22 07:24
咋不把错误信息贴出来呢
class Dog:
def __init__(self,name):
self.name=name
self.__age=None
def set_age(self,age):
if not isinstance(age,int):
print("请输入的年龄必须是数字!")
return False
if age <=0:
print("年龄必须大于零!")
return False
self.__age=age
def play(self):
print("今年"+self.__age)
dog=Dog("旺财")
dog.set_age('hello')
dog.set_age(-20)
dog.set_age(3)
dog.play()
SyntaxError: invalid syntax
>>>
>>>
>>>
================================================================== RESTART: C:/Users/owo/AppData/Local/Programs/Python/Python39/Dog.py ==================================================================
请输入的年龄必须是数字!
年龄必须大于零!
Traceback (most recent call last):
File "C:/Users/owo/AppData/Local/Programs/Python/Python39/Dog.py", line 19, in <module>
dog.play()
File "C:/Users/owo/AppData/Local/Programs/Python/Python39/Dog.py", line 14, in play
print("今年"+self.__age)
TypeError: can only concatenate str (not "int") to str 闲时 发表于 2021-5-22 13:10
class Dog:
def __init__(self,name):
self.name=name
print("今年"+self.__age)
这里字符串和数值不能直接用+连接
可以print("今年",self.__age)
也可以print("今年"+str(self.__age)) wp231957 发表于 2021-5-22 13:18
print("今年"+self.__age)
这里字符串和数值不能直接用+连接
可以print("今年",self.__age)
谢谢
页:
[1]