Home > Software design >  Syntactic error during the print of a class properties
Syntactic error during the print of a class properties

Time:11-24

It has been created a class which describes a person (name, address etc.). .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

CodePudding user response:

First of all, the constructor should be __init__ and not _init_
Second, you have an = inside the print function where you try to create the output string.
I suggest to use __str__ method:

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

CodePudding user response:

= 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:

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__.
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. Your function _init_ is a normal method of the class.

  • Related