Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Topic Defining Functions Learning Objectives 1. Understand the definition of fun

ID: 3606215 • Letter: T

Question

Topic Defining Functions Learning Objectives 1. Understand the definition of functions 2. Be able to use functions in programming 3. Understand parameters and how to use them 4. Understand and implement return values Activities Write a script with the following o a function that meets these specifications: squareEach(nums) nums is a list of numbers. o a function that meets these specifications: sumList(nums) nums is a list of numbers. o a function that meets these specifications: toNumbers(strList) strList is a list of strings, Modifies the list by squaring each entry Returns the sum of the numbers in the list each of which represents a number. Modifies each entry in the list by converting it to a number. o Uses the functions to implement a program that computes the sum of the squares of numbers read from a file. Your program should prompt for a file name and print out the sum of the squares of the values in the file. Hint: use readlines0. File will contain one number per line. A sample input file a4test.txt is included for testing purposes

Explanation / Answer

Hi,
Here is the full code with comments
def squareEach(nums):#function to square of each number in list
for i in range(0,len(nums)):
nums[i]= nums[i]*nums[i]
return nums
def sumList(nums):#function to return sum of numbers of list
sum=0
for i in nums:
sum+=i
return sum
def toNumbers(strList):#function to convert strings list to numbersd
for i in range(0,len(strList)):
strList[i]= int(strList[i])
return strList
name = raw_input("enter file name")#reading file name
f = open(name,'r')
with open('filename') as f:
lines = f.readlines()#getting line by line into list
f.close()
l=toNumbers(lines)#convering to numbers
l=squareEach(l)#squaring each number
print("the sum of squares is ",sumList(nums))#printng sum of list


Thumbs up if this was helpful, otherwise let me know in comments

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote