(1) Print a function square(x) that returns the square of the number x. Using this square function,...
Fantastic news! We've Found the answer you've been seeking!
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 of the squares of numbers from 12 + 22 + .... + n2 where n is input to the program (the main program will call the square function)
- (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
#######
- (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
- (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
Related Book For
Applied Regression Analysis and Other Multivariable Methods
ISBN: 978-1285051086
5th edition
Authors: David G. Kleinbaum, Lawrence L. Kupper, Azhar Nizam, Eli S. Rosenberg
Posted Date: