Im new to python. I learned java first, awhile back. But, Ive been learning linux lately and got into python. I couldnt make the code in the book to work. I had to change it after a few trial runs on powershell. Im pasting the before and after code (that I got to work) and the errors from PS. I should also mention Shaw is using python 3.6 and Im using 3.8. I didnt think there were many differences between the two but Im constantly finding more and more. What I want to know is why, after the constructor method, init(self,lyrics) creates the object do I have to pass lyrics to my method, AGAIN?. Also, is it the case where in every situation of instantiation the constructor takes no argument? The before and after code with PS output:
class Song(object):
def __init__(self, lyrics):
self.lyrics = lyrics
def sing_me_a_song(self):
for line in self.lyrics:
print(line)
happy_bday = Song(["Happy birthday to you",
"I don't want to get sued",
"So I'll stop right there"])
bulls_on_parade = Song(["They rally around tha family",
"With pockets full of shells"])
happy_bday.sing_me_a_song()
bulls_on_parade.sing_me_a_song()
Did not work for me. I am using python 3.8. Is it really that much of a difference? I finally got it to work, this way:(different apps, different fonts)
class Song(object):
def _init_(self,lyrics):
self.lyrics=lyrics
def sing_me_a_song(self,lyrics):
for line in lyrics:
print(line)
happy_bday = Song()
song1 = ["Happy birthday to you", "I don't want to get sued", "So I'll stop right there"]
bulls_on_parade = Song()
song2 = ["They rally around tha family", "With pockets full of shells"]
happy_bday.sing_me_a_song(song1)
bulls_on_parade.sing_me_a_song(song2)
Before, powershell was giving me some errors. The following is a transcript of all the output from all the changes before I got it right:
PS C:\Users\jharr\lpthw> python ex40.py Traceback (most recent call last):
File "ex40.py", line 9, in <module>
happy_bday = Song(["Happy birthday to you", "I don't want to get sued", "So I'll stop right there"])
TypeError: Song() takes no arguments
PS C:\Users\jharr\lpthw> python ex40.py File "ex40.py", line 13
happy_bday.sing_me_a_song(song1)
^
SyntaxError: invalid syntax
PS C:\Users\jharr\lpthw> python ex40.py File "ex40.py", line 13
happy_bday.sing_me_a_song()
^
SyntaxError: invalid syntax
PS C:\Users\jharr\lpthw> python ex40.py File "ex40.py", line 13
happy_bday.sing_me_a_song()
^
SyntaxError: invalid syntax
PS C:\Users\jharr\lpthw> python ex40.py File "ex40.py", line 14
happy_bday.sing_me_a_song(song1)
^
SyntaxError: invalid syntax
PS C:\Users\jharr\lpthw> python ex40.py Traceback (most recent call last):
File "ex40.py", line 9, in <module>
happy_bday = Song([])
TypeError: Song() takes no arguments
PS C:\Users\jharr\lpthw> python ex40.py Traceback (most recent call last):
File "ex40.py", line 14, in <module>
happy_bday.sing_me_a_song(song1)
TypeError: sing_me_a_song() takes 1 positional argument but 2 were given
PS C:\Users\jharr\lpthw> python ex40.py Traceback (most recent call last):
File "ex40.py", line 14, in <module>
happy_bday.sing_me_a_song(song1)
File "ex40.py", line 6, in sing_me_a_song
for line in self.lyrics:
AttributeError: 'Song' object has no attribute 'lyrics'
PS C:\Users\jharr\lpthw> python ex40.py Happy Happy birthday to you
I don't want to get sued
So I'll stop right there
Traceback (most recent call last):
File "ex40.py", line 16, in <module>
bulls_on_parage.sing_me_a_song(song2)
NameError: name 'bulls_on_parage' is not defined
PS C:\Users\jharr\lpthw> python ex40.py Happy Happy birthday to you
I don't want to get sued
So I'll stop right there
They rally around tha family
With pockets full of shells
PS C:\Users\jharr\lpthw>
PS C:\Users\jharr\lpthw> python ex40.py