Ask Question
6 May, 12:05

2. Write a standalone function partyVolume () that takes accepts one argument, a string containing the name of a file. The objective of the function is to determine the a Volume object that is the result of many people at a party turning the Volume up and down. More specifically:

+2
Answers (1)
  1. 6 May, 12:48
    0
    The Python code is given below with appropriate comments

    Explanation:

    #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
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “2. Write a standalone function partyVolume () that takes accepts one argument, a string containing the name of a file. The objective of the ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers