[Tutor] problem with program in python in easy steps
i wrote these programs and saved them per instructions on page 128 and 129 in the book "python in easy steps". class Person: '''A base class to define Person properties.''' def__init__(self,name): self.name = name def speak( self,msg = '(Calling The Base Class)'): print(self.name,msg) from Person import* '''A derived class to define Man properties.''' class Man(Person): def speak(self,msg): print(self.name,':\n\tHello!',msg) from Person import* '''A derived class to define Hombre properties.''' class Hombre(Person): def speak(self,msg): print(self.name,':\n\tHola!',msg) from Man import* from Hombre import* guy_1 = Man('Richard') guy_2 = Hombre('Ricardo') guy_1.speak('It\'s a beautiful evening.\n') guy_2.speak('Es una tarde hermosa.\n') Person.speak(guy_1) Person.speak(guy_2) i ran the program override.py and get this error message: Traceback (most recent call last): File "scripts/override.py", line 1, in from Man import* File "/home/chris/scripts/Man.py", line 2 '''A derived class to define Man properties.''' ^ IndentationError: unexpected indent ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] problem with program in python in easy steps
On 26/10/17 21:02, Chris Coleman wrote: > i wrote these programs and saved them per instructions on page 128 and 129 > in the book "python in easy steps". I don't know the book but... > > class Person: > '''A base class to define Person properties.''' > def__init__(self,name): > self.name = name > def speak( self,msg = '(Calling The Base Class)'): > print(self.name,msg) > I assume the book suggests storing these class definitions in separate files named after the classes? (A small point is that usual style is to name modules in all lower case) > from Person import* > '''A derived class to define Man properties.''' The string should start at the beginning of the line - it is not inside a new block so should not be indented. I assume the book recommends the from x import * style but thats considered bad practice. Only import the names you need or, if there are many, import just the module name (possibly with an alias). Like so: from modulename import name1, name2,etc... import modulename import modulename as alias The import * style can lead to weird bugs due to name collisions. > class Man(Person): > def speak(self,msg): > print(self.name,':\n\tHello!',msg) > > from Person import* > '''A derived class to define Hombre properties.''' Same problem with the string definition. > class Hombre(Person): > def speak(self,msg): > print(self.name,':\n\tHola!',msg) > > from Man import* > from Hombre import* > guy_1 = Man('Richard') > guy_2 = Hombre('Ricardo') > guy_1.speak('It\'s a beautiful evening.\n') > guy_2.speak('Es una tarde hermosa.\n') > Person.speak(guy_1) > Person.speak(guy_2) > > i ran the program override.py and get this error message: > > Traceback (most recent call last): > File "scripts/override.py", line 1, in > from Man import* > File "/home/chris/scripts/Man.py", line 2 > '''A derived class to define Man properties.''' > ^ > IndentationError: unexpected indent And this is telling you not to indent your string. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] problem with program in python in easy steps
On Thu, Oct 26, 2017 at 3:02 PM, Chris Coleman wrote: > > i wrote these programs and saved them per instructions on page 128 and 129 > in the book "python in easy steps". > > class Person: > '''A base class to define Person properties.''' > def__init__(self,name): The above line should generate an error as there is no space between "def" and "__init__". > self.name = name > def speak( self,msg = '(Calling The Base Class)'): > print(self.name,msg) > > from Person import* > '''A derived class to define Man properties.''' > class Man(Person): > def speak(self,msg): > print(self.name,':\n\tHello!',msg) > > from Person import* > '''A derived class to define Hombre properties.''' > class Hombre(Person): > def speak(self,msg): > print(self.name,':\n\tHola!',msg) > > from Man import* > from Hombre import* > guy_1 = Man('Richard') > guy_2 = Hombre('Ricardo') > guy_1.speak('It\'s a beautiful evening.\n') > guy_2.speak('Es una tarde hermosa.\n') > Person.speak(guy_1) > Person.speak(guy_2) > > i ran the program override.py and get this error message: > > Traceback (most recent call last): > File "scripts/override.py", line 1, in > from Man import* > File "/home/chris/scripts/Man.py", line 2 > '''A derived class to define Man properties.''' > ^ > IndentationError: unexpected indent So, did you try removing the indentation in front of '''A derived class ... '''? And once you do, you will get the same error later on where you did the same thing. Recall that Python uses indentation to define its code blocks. -- boB ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor