write a python function called isvalidscore. it should accept 3 parameters: scor
ID: 3792281 • Letter: W
Question
write a python function called isvalidscore. it should accept 3 parameters: score, lowerlimit, upperlimit
description: the function checks to see if it is between and/or including the range from lowerlimit to upperlimit. it return true if the score is in the specified range and returns false if the score is not within the specified range. Note: there should not be any prin statements in the function! Examples of calling the function:
print(isvalidscore(97,0,100)) #True
print(isvalidscore(-5,0,100)) #False
print(isvalidscore(100,0,100)) #True
print(isvalidscore(0,0,100)) #True
---------------------------------------------------------------------------
supose myscores contains a list of numbers. Write python code to get rid of all scores below 60 in mylist.
example: if myscores contains[20,100,88,40,50,90]. The result should be [100,88,90]
Explanation / Answer
Answer 1:
In the first solution directly the result is returned in the 'return' statement only by combining the condition using 'and'.
def isvalidscore(score, lowerlimit, upperlimit):
return score>=lowerlimit and score<=upperlimit;
print(isvalidscore(97,0,100))
print(isvalidscore(-5,0,100))
print(isvalidscore(100,0,100))
print(isvalidscore(0,0,100))
=========================================================================
Answer 2:
''filterout()' function is performing the required operation i.e. iterating through the list and checking the entry with the given condition (i.e. > 60 in this case) and appending to a new list which is returned in the end of the function. (Good programming practice : Use of function improves modularity in the program thus implementing segregation of functionalities for larger programs)
def filterout(inputList):
returnlist = []
for number in inputList:
if number > 60:
returnlist.append(number)
return returnlist
myscores = [20,100,88,40,50,90]
print(filterout(myscores))
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.