Bottom Page

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!
buran wrote May-09-2019, 11:30 AM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Quote
#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
Quote
#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?
Quote
#4
Change the default value for duration in the __init__():
def __init__(self, artist, title, duration=Duration(0, 0)):
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  EOFError: EOF when reading a line - Runtime Error RavCOder 6 329 Sep-27-2019, 12:22 PM
Last Post: RavCOder
  Project, Reading Data from a spreadsheet. Error message!! Shafla 1 101 Sep-27-2019, 10:44 AM
Last Post: buran
  Reading DBF files from Amazon s3 throwing error - 'int' object has no attribute 'isa abeesm 1 167 Sep-22-2019, 05:49 PM
Last Post: ndc85430
  Type Error: bad operand type for unary +: 'str' Psypher1 2 843 Aug-12-2019, 02:30 PM
Last Post: Psypher1
  catch input type error mcmxl22 5 250 Aug-11-2019, 07:33 AM
Last Post: wavic
  Pulling & Reading Date from UDF that Compare it to Live Data firebird 4 241 Jul-20-2019, 09:30 AM
Last Post: snippsat
  Type error: dump() missing 1 required positional argument: fp jaycuff13 2 1,265 Jul-13-2019, 10:21 AM
Last Post: jaycuff13
  Changing Data Types BallisticSwami 2 217 Jun-27-2019, 01:17 PM
Last Post: BallisticSwami
  Reading JSON - error jmair 2 208 May-22-2019, 07:25 PM
Last Post: jmair
  Reading data from serial port as byte array vlad93 1 1,547 May-18-2019, 05:26 AM
Last Post: heiner55

Forum Jump:


Users browsing this thread: 1 Guest(s)