In: Computer Science
##### Volume #####
# set and get
>>> v = Volume()
>>> v.set(5.3)
>>> v
Volume(5.3)
>>> v.get()
5.3
>>> v.get()==5.3 # return not print
True
>>>
# __init__, __repr__, up, down
>>> v = Volume(4.5) # set Volume with value
>>> v
Volume(4.5)
>>> v.up(1.4)
>>> v
Volume(5.9)
>>> v.up(6) # should max out at 11
>>> v
Volume(11)
>>> v.down(3.5)
>>> v
Volume(7.5)
>>> v.down(10) # minimum must be 0
>>> v
Volume(0)
# default arguments for __init__
>>> v = Volume() # Volume defaults to 0
>>> v
Volume(0)
# can compare Volumes using ==
>>> # comparisons
>>> v = Volume(5)
>>> v.up(1.1)
>>> v == Volume(6.1)
True
>>> Volume(3.1) == Volume(3.2)
False
# constructor cannot set the Volume greater
# than 11 or less than 0
>>> v = Volume(20)
>>> v
Volume(11)
>>> v = Volume(-1)
>>> v
Volume(0)
>>>
##### partyVolume #####
>>> partyVolume('party1.txt')
Volume(4)
>>> partyVolume('party2.txt')
Volume(3.75)
>>> partyVolume('party3.txt')
Volume(0.75)
# make sure return not print
>>> partyVolume('party1.txt')==Volume(4) # return not print
True
>>> partyVolume('party2.txt')==Volume(3.75)
True
>>> partyVolume('party3.txt')==Volume(0.75)
True
use python3.7
Here is the completed code for this problem. Since you did not provide the Volume class, I have written this code based on my assumptions and doctests. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
Note: Please maintain proper code spacing (indentation), just copy the code part and paste it in your compiler/IDE directly, no modifications required.
#code
#required method, assuming Volume class is defined and is
accessible
def partyVolume(filename):
#opening file in read mode, assuming file
exists
file=open(filename,'r')
#reading initial volume
initial=float(file.readline())
#creating a Volume object with initial
volume
v=Volume(initial)
#looping through rest of the lines in
file
for line
in file.readlines():
#removing
trailing/leading white spaces/newlines and splitting line by
white
#space to get a list of
tokens
line=line.strip().split(' ')
#ensuring that
length of resultant list is 2
if
len(line)==2:
#reading first value as direction (U or D)
dir=line[0].upper()
#reading second value as float value
value=float(line[1])
if dir=='U':
#turning volume up
v.up(value)
elif dir=='D':
#turning volume down
v.down(value)
#closing file, saving changes
file.close()
#returning volume
return v