Question: (1) Print a function square(x) that returns the square of the number x. Using this square function, create a Python program to compute the sum

  1. (1) Print a function square(x) that returns the square of the number x. Using this square function, create  a Python program to compute the sum of the squares of numbers from 12 + 22 + .... + n2 where n is input to the program (the main program will call the square function)

 

  1. (2) Print a program that prompts the user to enter a character ch and a number length. The program will print on one line the character ch repeated length times. For this, the program will call the function draw_line(). You must create the function draw_line(char,nbr) that returns a string with the character char repeated nbr times.

Sample runs: 

This program will draw a line for you. 

Enter a character: * 

Enter the length of the line you want to draw: 5 

***** 

This program will draw a line for you. 

Enter a character: # 

Enter the length of the line you want to draw: 7 

####### 

 

  1. (3) Print a program to omit the occurrences of a letter from a string. The program will prompt the user for a letter and a string. create  a function remove_letter(theLetter, theString) in that program that traverses the string to search for the input or target letter, and to remove it from the string once found. Hint: use a new blank string and concatenate the letters from the original input string into the new string. Your function will then return the new string that will now contain the letters of the original string except the occurrences of the target letter. The main program will input the string and the letter to be removed, call the function and then print the string with the letter removed.

Sample run: Enter a letter: L 

Enter a string: LOL 

The string after removing letter L is O

 

  1. (4) Implement a function that takes a list of numbers as a parameter. Your function will compute the average of all the values in the list. Your function will then return the average. In the main program, you should call the function twice and pass to it each of the below lists that are initialized in the main program, one list in each call. Do not use Python built-in functions. Your function definition should begin as follows:

# main program 

intNumbers = [8,7,5,6,9,3,2,4] 

realNumbers = [1.1, 5.4, 6.8, 9.3, 4.2, 3.8, 12.5] 

....

 

Sample Run:

The average = 5.5

The average = 6.1571428571428575 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

Python programs for each of the four tasks you mentioned Task 1 Compute the Sum of Squares python de... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!