[英]Syntactic error during the print of a class properties
It has been created a class which describes a person (name, address etc.).它已经创建了一个 class 来描述一个人(姓名、地址等)。 .It must be printing all the properties whichThe code it is in the follow lines .它必须打印以下代码中的所有属性
class Person:
personNo=0
def _init_(unit,first_name,last_name,age,address):
unit.personsfirstname=first_name
unit.lastname=last_name
unit.personsage=age
unit.address=address
unit.personNo+=1
def ShowInfo(unit):
print("Ονομα: "+unit.personsfirstname+ "Επιθετο: "+unit.lastname+ "Ηλικια: "+unit.personsage+ "Οδος: "+unit.address=address)#The problem is here
person1=Person("George","Stefanopoulos","55","Alpha street 33")
person2=Person("Μαρια","Πενταγιωτισα","25","Παπαλαμπρενας 28")
So, when the code is executed it is appeared error: expression can not contain assignment perhaps you meant "=="?所以,当代码执行时出现错误:表达式不能包含赋值也许你的意思是“==”? .The problem it is appeared where is located the note. .它出现的问题在哪里。 It must be noted that this code is based another example code which works perfectly必须注意,此代码基于另一个完美运行的示例代码
=
is used to assign value. =
用于赋值。
Supposing you want to display the address as you did for the name, try like this:假设您想像显示名称一样显示地址,请尝试如下:
class Person:
personNo=0
def __init__(unit,first_name,last_name,age,address):
unit.personsfirstname=first_name
unit.lastname=last_name
unit.personsage=age
unit.address=address
unit.personNo+=1
def ShowInfo(unit):
print("Ονομα: "+unit.personsfirstname+ "Επιθετο: "+unit.lastname+ "Ηλικια: "+unit.personsage+ "Οδος: "+ unit.address)
person1=Person("George","Stefanopoulos","55","Alpha street 33")
person2=Person("Μαρια","Πενταγιωτισα","25","Παπαλαμπρενας 28")
If you want to simply return True
or False
for address, but not sure why would you do that, do this:如果您只想为地址返回True
或False
,但不确定为什么要这样做,请执行以下操作:
class Person:
personNo=0
def __init__(unit,first_name,last_name,age,address):
unit.personsfirstname=first_name
unit.lastname=last_name
unit.personsage=age
unit.address=address
unit.personNo+=1
def ShowInfo(unit):
print("Ονομα: "+unit.personsfirstname+ "Επιθετο: "+unit.lastname+ "Ηλικια: "+unit.personsage+ "Οδος: "+str(unit.address==address))
person1=Person("George","Stefanopoulos","55","Alpha street 33")
person2=Person("Μαρια","Πενταγιωτισα","25","Παπαλαμπρενας 28")
Also, you used _init_
and the constructor can be defined using __init__
.此外,您使用了_init_
并且可以使用__init__
定义构造函数。
Not having the right way declared constructor, you're getting an error for trying to create a new instance of class Person
with some arguments.如果没有正确的方法声明构造函数,您会在尝试使用一些 arguments 创建 class Person
的新实例时遇到错误。 Your function _init_
is a normal method
of the class.你的 function _init_
是 class 的正常method
。
First of all, the constructor should be __init__
and not _init_
首先,构造函数应该是__init__
而不是_init_
Second, you have an =
inside the print
function where you try to create the output string.其次,您在print
function 中有一个=
,您可以在其中尝试创建 output 字符串。
I suggest to use __str__
method:我建议使用__str__
方法:
class Person:
personNo=0
def __init__(self,first_name,last_name,age,address):
self.personsfirstname=first_name
self.lastname=last_name
self.personsage=age
self.address=address
self.personNo+=1
def __str__(self):
s = "Ονομα: "+self.personsfirstname+ " Επιθετο: "+self.lastname+ " Ηλικια: "+self.personsage+ " Οδος: "+self.address
return s
person1=Person("George","Stefanopoulos","55","Alpha street 33")
person2=Person("Μαρια","Πενταγιωτισα","25","Παπαλαμπρενας 28")
# thanks to __str__ method, you can do:
print(person1)
Ονομα: George Επιθετο: Stefanopoulos Ηλικια: 55Ο δος: Alpha street 33
print(person2)
Ονομα: Μαρια Επιθετο: Πενταγιωτισα Ηλικια: 25Οδος: Παπαλαμπρενας 28
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.