请帮看一下错误:class Person(object): def int(self,name,age): self.id=1 self.name="name" self.age=18 def doing(self): print('我的学号:%a,姓名:%b,年龄:%c'%(self,self.name,self.age)) new_Person=Person() print(new_Person.id)
时间: 2024-02-26 21:56:52 浏览: 22
这段代码有几个错误。首先,`int`应该是`__init__`,用于初始化对象。其次,`self.name`和`self.age`的值应该是传入的参数`name`和`age`,而不是字符串`"name"`和整数`18`。最后,`%a`、`%b`和`%c`应该分别是`%d`、`%s`和`%d`。
正确的代码如下:
```python
class Person:
def __init__(self, name, age):
self.id = 1
self.name = name
self.age = age
def doing(self):
print('我的学号:%d,姓名:%s,年龄:%d' % (self.id, self.name, self.age))
new_person = Person("张三", 20)
print(new_person.id)
new_person.doing()
```
这段代码定义了一个名为`Person`的类,其中`__init__`方法用于初始化对象的属性,`doing`方法用于打印对象的属性。创建了一个名为`new_person`的`Person`对象,并分别打印了其`id`属性,以及调用了`doing`方法打印了其所有属性。
相关问题
class Person: def __init__(self, name, age): self.name = name self.age = age
def introduce(self):
print(f"My name is {self.name} and I am {self.age} years old.")
class Student(Person):
def __init__(self, name, age, major):
super().__init__(name, age)
self.major = major
def study(self):
print(f"I am studying {self.major}.")
class Teacher(Person):
def __init__(self, name, age, subject):
super().__init__(name, age)
self.subject = subject
def teach(self):
print(f"I am teaching {self.subject}.")
s = Student("John", 20, "Computer Science")
s.introduce() # Output: My name is John and I am 20 years old.
s.study() # Output: I am studying Computer Science.
t = Teacher("Jane", 35, "Math")
t.introduce() # Output: My name is Jane and I am 35 years old.
t.teach() # Output: I am teaching Math.
查看代码错误:class Person(object): def __init__(self, name, age): self.id = 1 self.name = name self.age = age def doing(self): print(f"{我的学号:self.id,姓名:self.name,年龄:self.age}") new_person = Person("张三", 20) # print(new_person.id) new_person.doing()
这段代码存在一个语法错误,即在 `print` 函数中使用了 f-string,但是 f-string 中的花括号中没有写变量名。应该将 `self.id, self.name, self.age` 加上花括号,代码如下:
```python
class Person(object):
def __init__(self, name, age):
self.id = 1
self.name = name
self.age = age
def doing(self):
print(f"我的学号:{self.id}, 姓名:{self.name}, 年龄:{self.age}")
new_person = Person("张三", 20)
new_person.doing()
```
这样就可以正确地输出 `我的学号:1, 姓名:张三, 年龄:20`。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)