Python Forum
Type error when reading in different data types on an __init__ method
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Type error when reading in different data types on an __init__ method
#1
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!
Reply
#2
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
Reply
#3
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?
Reply
#4
Change the default value for duration in the __init__():
def __init__(self, artist, title, duration=Duration(0, 0)):
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  What data types can I use for default values? Mark17 1 522 Oct-09-2023, 02:07 PM
Last Post: buran
  Wrong type error rowan_bradley 6 1,214 Aug-07-2023, 10:44 AM
Last Post: rowan_bradley
  xlwings error when reading a workbook Mishal0488 1 1,099 Aug-01-2023, 02:05 AM
Last Post: deanhystad
  Type Error: Unsupported Operand jhancock 2 1,170 Jul-22-2023, 11:33 PM
Last Post: jhancock
  reading a table which is of type string saisankalpj 2 957 Dec-03-2022, 11:19 AM
Last Post: saisankalpj
  Reading All The RAW Data Inside a PDF NBAComputerMan 4 1,341 Nov-30-2022, 10:54 PM
Last Post: Larz60+
  i want to use type= as a function/method keyword argument Skaperen 9 1,838 Nov-06-2022, 04:28 AM
Last Post: Skaperen
  Reading Data from JSON tpolim008 2 1,077 Sep-27-2022, 06:34 PM
Last Post: Larz60+
  In SQL Server, mix data types. shiv11 0 876 Sep-21-2022, 12:50 PM
Last Post: shiv11
  I need to add data types to cython conversion python to c Good_AI_User 1 997 Aug-19-2022, 07:52 AM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020