Question

Objectives Work with functions Assignment Write each of the following functions using Python. The function header MUST b...

Objectives

Work with functions

Assignment

Write each of the following functions using Python. The function header MUST be written as specified. In your main code test all of the specified functions. Each function must have a comment block explaining what it does, what the parameters are and what the return value is.

Please remember the following two guidelines:

  • unless the purpose of the function is to generate output DO NOT write to the

    screen within the function

  • unless the purpose of the function is to ask the user for a value DO NOT ask the

    user for a value within the function

    Required functions

    def inputWithinRange (lowValue, highValue):
    

    This function will ask for a number from the keyboard. The function will only return a value that is within the inclusive range of lowValue to highValue. If the entered value is not within the provided range, the function will ask again until the user has entered an acceptable value. The function will return the accepted value.

    def doContinue (prompt):
    This function will ask for a string using the prompt parameter. The function will returnTrue if the user enters an upper or lower case “yes” or False if the user enters an upper or lower case “no”. Any other entered value will cause the function to ask again until an acceptable value has been entered.

    def square (intValue):
    This function returns the square of intValue.

    def summation (intValue):
    This function returns the summation of 1 to intValue. You can assume thatintValue will be positive. For example, summation (5) would return 15 (1 + 2 + 3 + 4 + 5).

    def sumOfSquare (intValue):
    This function return the sum of the squares from 1 to intValue. For example,sumOfSquares(5) would return 55 (1 + 4 + 9 + 16 + 25). You MUST use a loop and the square function to determine the returned value.

    def factorial (intValue):
    This function returns the factorial of 1 to intValue. You can assume that intValuewill be positive. For example, factorial (5) would return 120 (1 * 2 * 3 * 4 * 5).

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code with indentation:

1 Function

7#function asks user 8#if not it will calL ask user to enter value and Loop 9 def inputWithRange ( lowVa lue, highValue) n in

2 Function

function to check whether user entered yes or no 21 def doContinue (prompt): 20 # 22 if(promptYES or prompt==yes ): 23 24

3,4&5 functions

33 34 # finds square 35 def square(intValue) 36 return intValue**2 37 print (square(4)) 38 39#finds summation of values till

6th function

55#finds factorial of number 56 def factorial (intValue) fact=1 57 for i in range(1,intValue+1) fact-fact*i 58 59 60 return f

_____________________________________________________________________________________

Outputs:

1st function output

Console 1/A In [12]: def inputWithRange (lowValue,highValue) n=int(input(Enter value: )) while(n>highValue or n<lowValue) n

2nd function output

In [13] def doContinue (prompt): if (prompt==YES or prompt==yes) return True elif(prompt== NO or prompt==no): return

3rd function output

In [14] def square (intValue) return intValue*2 print (square (4)) 16 In [15]: Help Source Console Object

4th function output:

In [15] def summation (intValue) sumtn-0 for i in range(1,intValue+1) sumtn-sumtn+i return sumtn print(summation (5)) 15 In [

5th function output

In [16] def sumOfsquare (intValue) sumtn-0 for i in range(1,intValue+1) sumtn sumtn+(i**2) return sumtn ..: print(sumofsquare

6th function output:

In [17]: def factorial(intValue): fact-1 for i in range (1, intValue+1): fact-fact*i return fact print(factorial(5)) 120 In [

___________________________________________________________________________________________

Add a comment
Know the answer?
Add Answer to:
Objectives Work with functions Assignment Write each of the following functions using Python. The function header MUST b...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Required functions def inputWithinRange (lowValue, highValue): This function will ask for a number from the keyboard....

    Required functions def inputWithinRange (lowValue, highValue): This function will ask for a number from the keyboard. The function will only return a value that is within the inclusive range of lowValue to highValue. If the entered value is not within the provided range, the function will ask again until the user has entered an acceptable value. The function will return the accepted value. def doContinue (prompt): This function will ask for a string using the prompt parameter. The function will...

  • PYTHON. Write the following functions without using any predefined functions unless specified. def min (dataList) :      ...

    PYTHON. Write the following functions without using any predefined functions unless specified. def min (dataList) :       #returns the smallest value of the data values in dataList def max (dataList):        #returns the largest value def getRange (dataList) def mean (dataList) :     #returns the average of data values def median (dataList):    #returns the middle value of an ordered list #note: to sort the list first, may use the predefined sort function Then write a test1 function to test the above functions using...

  • Using Python 3+ for Question P5.9 Instructions: Use one main() to call each of the functions...

    Using Python 3+ for Question P5.9 Instructions: Use one main() to call each of the functions you created. Only use one value of r and h for all the function. Ask the user for the r and h in the main() as an input, and h for all the functions. You should put the functions for areas in a separate file. ​ For example, firstDigtec7ze Write a function e digits n) (returning the number of digits in the argument returning...

  • USING PYTHON PLEASE Write a program that calculates the factorial value of a number entered by...

    USING PYTHON PLEASE Write a program that calculates the factorial value of a number entered by the user. Remember that x! =x* (x-1)* (x-2)*... *3+ 2* 1. Your program should check the value input by the user to ensure it is valid (i.e., that it is a number > =1). To do this, consider looking at the is digit() function available in Python. If the user enters an incorrect input, your program should continue to ask them to enter a...

  • Functions can return a string, not just an int or a float. Write a function called...

    Functions can return a string, not just an int or a float. Write a function called everOrOdd which takes an integer parameter and returns either "Even" or "Odd" based on the value passed. In main, prompt the user for a number, called num, then pass num to evenOrOdd and display the returned value on the screen. Keep doing this until the user enters a zero. Use the following run as an example: Enter a number: 11 Odd Enter a number:...

  • in Python: Write a program that allows the user to enter a number between 1-5 and...

    in Python: Write a program that allows the user to enter a number between 1-5 and returns an animal fact based on what the user entered. The program should 1. Continue to run until the user enters “quit” to terminate the program, and 2. Validate the user’s input (the only acceptable input is 1, 2, 3, 4, 5 or “quit”). You can use the following animal facts: An octopus is a highly intelligent invertebrate and a master of disguise. Elephants...

  • Python Programming language Write the following functions: getNumInRange(prompt, min, max) - get...

    Python Programming language Write the following functions: getNumInRange(prompt, min, max) - gets a number from the user within the specified range, prompting with 'prompt' calcAvg(values) - calculates and returns the average from a supplied list of numeric values menu(somelist) - creates a numbered menu from a list of strings. Then, have the user select a valid choice (number), and return it. Use the getNumInRange() funtion to do this. getAlbum() - prompts the user for information to populate a dictionary representing...

  • Factorial.java This is an exercise in using repetition structures to calculate the mathematical values of the...

    Factorial.java This is an exercise in using repetition structures to calculate the mathematical values of the factorial function. Since methods have not yet been covered in detail, you may do the calculations in a class that contains just a main method. The looping structures require the use of count-controlled loops. This program computes the factorial function of a non-negative integer, which is written mathematically as n! (the parameter n followed by an exclamation mark). For values of n equal to...

  • using python Do it now 2. Write a code with two functions of squared and square_root....

    using python Do it now 2. Write a code with two functions of squared and square_root. At the beginning your code should ask the user to enter one number. Then it should ask the user what they want to do (enter s to calculate the squared value of the entered number and enter r to perform a square root). Then based on the choice of user (using if) the program should call the proper function. Each function, when executing, should...

  • Hi. Could you help me write the below program? Please don't use any header other than...

    Hi. Could you help me write the below program? Please don't use any header other than iostream, no Chegg class, no argc/argv. Nothing too advanced. Thank you! For this assignment you are implement a program that can be used to compute the variance and standard deviation of a set of values. In addition your solution should use the functions specified below that you must also implement. These functions should be used wherever appropriate in your solution. With the exception of...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT