Python Forum
Type error when reading in different data types on an __init__ method - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Type error when reading in different data types on an __init__ method (/thread-18210.html)



Type error when reading in different data types on an __init__ method - Dylanmull - May-09-2019

I have the following task to complete:
Making appropriate use of your Duration class write an MP3Song class to
model an MP3 song. Every song has an artist, a title and a duration. (Your
class must inherit only from object.) When your class is correctly
implemented, running the program below will produce the output that follows:

from exam_2018 import Duration, MP3Song
d = Duration(6, 31)
s1 = MP3Song('U2', 'Zooropa', d)
s2 = MP3Song('U2', 'Ultraviolet')
s3 = MP3Song('The National', 'Lucky You', Duration(3, 56))
print(s1)
print(s2)
print(s3)
Expected output:

Output:
$ python3 mp3song_demo.py U2 : Zooropa (06:31) U2 : Ultraviolet (00:00) The National : Lucky You (03:56)
I am having trouble reading from s1 and s3 and the following code is what i have so far:

class Duration(object):

	def __init__(self, minutes=00, seconds=00):
		self.minutes = minutes
		self.seconds = seconds

	def __str__(self):
		return("{:02d}:{:02d}".format(self.minutes, self.seconds))

	def __gt__(self, other):
		return self.time_to_seconds() > other.time_to_seconds()

	def __eq__(self, other):
		return self.time_to_seconds() == other.time_to_seconds()

	def time_to_seconds(self):
		return(self.minutes * 60 + self.seconds)

class MP3Song(object):

	def __init__(self, artist, title, duration=0):
		self.artist = artist
		self.title = title
		self.duration = duration

	def __str__(self):
		return("{:} : {:} ({:02d}:{:02d})".format(
			self.artist, self.title, self.duration, self.duration))
I would greatly appreciate any help!


RE: Type error when reading in different data types on an __init__ method - avorane - May-09-2019

Hello,

When you print the result, you call twice duration. However, you have still defined a print fonction in the class Duration. So:

class Duration(object):

    def __init__(self, minutes=00, seconds=00):
        self.minutes = minutes
        self.seconds = seconds

    def __str__(self):
        return ("{:02d}:{:02d}".format(self.minutes, self.seconds))

    def __gt__(self, other):
        return self.time_to_seconds() > other.time_to_seconds()

    def __eq__(self, other):
        return self.time_to_seconds() == other.time_to_seconds()

    def time_to_seconds(self):
        return (self.minutes * 60 + self.seconds)


class MP3Song(object):

    def __init__(self, artist, title, duration=0):
        self.artist = artist
        self.title = title
        self.duration = duration

    def __str__(self):
        return ("{:} : {:} ({})".format(
            self.artist, self.title, self.duration))
Best Regards,

Nicolas TATARENKO


RE: Type error when reading in different data types on an __init__ method - Dylanmull - May-09-2019

Thanks for your help with that. It solved the problem with s1 and s3 but now that the format() has been changed the expected output for s2 has changed. The expected output for s2 is as follows:

Output:
U2 : Ultraviolet (0)
The expected standard output should be:

Output:
U2 : Ultraviolet (00:00)
Any tips on how to fix this without running into my previous problem?


RE: Type error when reading in different data types on an __init__ method - buran - May-09-2019

Change the default value for duration in the __init__():
def __init__(self, artist, title, duration=Duration(0, 0)):