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
  Help with WebSocket reading data from anoter function korenron 0 125 Sep-19-2021, 11:08 AM
Last Post: korenron
  Fastest Way of Writing/Reading Data JamesA 1 363 Jul-27-2021, 03:52 PM
Last Post: Larz60+
  Incorrect Type Error milkycow 4 495 Jun-25-2021, 06:04 AM
Last Post: milkycow
  pandas.errors.ParserError: Error tokenizing data. C error: Expected 9 fields in line Anldra12 9 1,748 Jun-15-2021, 08:16 AM
Last Post: Anldra12
  Reading data to python: turn into list or dataframe hhchenfx 2 553 Jun-01-2021, 10:28 AM
Last Post: Larz60+
  Reading data from mysql. stsxbel 2 567 May-23-2021, 06:56 PM
Last Post: stsxbel
Star Type Error: 'in' object is not callable nman52 3 670 May-01-2021, 11:03 PM
Last Post: nman52
  Error : "can't multiply sequence by non-int of type 'float' " Ala 3 759 Apr-13-2021, 10:33 AM
Last Post: deanhystad
  blank graph with matplotlib from a csv file / data type issue arsentievalex 0 454 Apr-06-2021, 10:08 AM
Last Post: arsentievalex
  Type Error in Python MarcusB 3 619 Mar-30-2021, 06:34 PM
Last Post: buran

Forum Jump:

User Panel Messages

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