Question

Function Example: Write a Python function that receives two integer arguments and writes out their sum...

Function Example: Write a Python function that receives two integer arguments and writes out their sum and their product. Assume no global variables.

def writer(n1, n2):

sum = n1 + n2

product = n1*n2

print("For the numbers", n1, "and", n2)

print("the sum is", sum)

print("and the product is", product)

...

1) Create a PYHW2 document that will contain your algorithms in flowchart and pseudocode form along with your screen shots of the running program.

2) Create the algorithm in both flowchart and pseudocode forms for the following two functions:

A. Write a Python function that receives a real number argument representing the sales amount for videos rented so far this month. The function asks the user for the number of videos rented today and returns the updated sales figure to the main function. All videos rent for $4.25.

B. Write a Python function that receives three integer arguments and returns the maximum of the three values.


3) Create the algorithm in both flowchart and pseudocode forms for a main program that tests the two above functions.


C. Write a Python main program that calls both above functions to shows that both functions work properly.


Programming Notes:


Function Definition -- def functionName( parameter list )

receives -- this denotes a value that is passed to the function as a parameter.

ask the user -- this denotes a value that the functions requests of the user.

return -- this denotes a value that is returned from the function to the calling program.


program including both functions and the main program in one python source file.

Homework Answers

Answer #1

'''
Python version : 3.6
Python program to create and test 2 functions
'''

def calculateSales(amount):
   '''
   Function that receives a real number argument representing the sales amount for videos rented so far this month
   The function asks the user for the number of videos rented today and returns the updated sales figure to the main function.
   All videos rent for $4.25.
   '''
  
   # input the number of videos rented today
   rented = int(input('Enter the number of videos rented today: '))
   # return the updated sales
   return(amount + (rented*4.25))
  
def maximum(value1, value2, value3):
   '''
   Function that receives three integer arguments and returns the maximum of the three values.
   '''
  
   # set max_value to value1
   max_value = value1
  
   # if value2 > max_value, update max_value to value2
   if value2 > max_value:
       max_value = value2
      
   # if value3 > max_value, update max_value to value3
   if value3 > max_value:
       max_value = value3
      
   return max_value  
  
# test the functions
sales = calculateSales(0)
print('Updated sales: %.2f' %sales)
sales = calculateSales(sales)
print('Updated sales: %.2f' %sales)
print('Maximum of 35, 70, 55: %d' %(maximum(35,70,55)))  

#end of program  

Code Screenshot:

Output:

Know the answer?
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
Similar Questions
c++ Write a program that calls a function calculateSum to calculate the sum from -1 to...
c++ Write a program that calls a function calculateSum to calculate the sum from -1 to N. The function calculateSum has one parameter N of type integer and returns an integer which represents the sum from -1 to N, inclusive. Write another function calculateAverage that calculates an average. This function will have two parameters: the sum and the number of items. It returns the average (of type float). The main function should be responsible for all inputs and outputs. Your...
Write a function that takes two integer inputs and returns the sum of all even numbers...
Write a function that takes two integer inputs and returns the sum of all even numbers between these inputs, and another function that takes two integer inputs and returns the sum of odd numbers between these inputs .In main function, the program will asks the user to enter two integer numbers and then passes them to these two functions and display the result of each of them.         [0.5 mark] (BY USING C PROGRAM)
TO BE DONE IN PYTHON: Write a function `lowest_integer()` which takes 2 input arguments: 1)a function...
TO BE DONE IN PYTHON: Write a function `lowest_integer()` which takes 2 input arguments: 1)a function `g` representing an increasing function g(x) 2) a number `gmin`, and returns an integer `nmin` such that nmin>0 is the smallest integer that satisfies g(nmin)>gmin. test: def g(n): return 2*n print(lowest_integer(g, 10)) Output: 6
IN PYTHON : Write a program that asks the user for a number. Write the number...
IN PYTHON : Write a program that asks the user for a number. Write the number to a file called total.txt. Then ask the user for a second number. Write the new number on line 2 with the total for both numbers next to it separated by a comma. Then ask the user for a third number and do the same. Keep asking for numbers until the person puts in a zero to terminate the program. Close the file. Be...
Write the functions which are called in the main function given. YOU MAY ASSUME THAT THE...
Write the functions which are called in the main function given. YOU MAY ASSUME THAT THE USER ENTERS A VALID INTEGER. printDouble should accept one value and should print the value of that number times 2. printEndMessage should not accept any values and should print a message indicating that the program has finished. def main(): num1 = int(input("Please enter your number ")) printDouble(num1) printEndMessage() main()
1. Write a function called compute_discount which takes a float as the cost and a Boolean...
1. Write a function called compute_discount which takes a float as the cost and a Boolean value to indicate membership. If the customer is a member, give him/her a 10% discount. If the customer is not a member, she/he will not receive a discount. Give all customers a 5% discount, since it is Cyber Tuesday. Return the discounted cost. Do not prompt the user for input or print within the compute_discount function. Call the function from within main() and pass...
Write spim program and execute it on mars. Your program reads two integer values x and...
Write spim program and execute it on mars. Your program reads two integer values x and y. Write a function called sum that gets x and y passed as parameters and return the sum of odd values between x and y inclusive. In addition write another function called even that gets x and y as parameters and returns the count of all even numbers between x and y inclusive. Also in main print the quotient and remainder of diving y...
Using python 3.5 or later, write the following program. A kidnapper kidnaps Baron Barton and writes...
Using python 3.5 or later, write the following program. A kidnapper kidnaps Baron Barton and writes a ransom note. It is not wrriten by hand to avoid having his hand writing being recognized, so the kid napper uses a magazine to create a ransom note. We need to find out, given the ransom note string and magazine string, is it possible to given ransom note. The kidnapper can use individual characters of words. Here is how your program should work...
float sum( float x, float y, float z ) ​program in c++
Here are the function prototypes for your homeworkWrite out the actual functions themselves Also, create a main function that demonstrates that all four functions work The user should be able to provide four values to your program (three floats and an int) Your program should then use your four new functions and also display the results to the user NONE of these functions should print values themselvesfloat sum( float x, float y, float z ); // returns the sum of three floatsfloat mean( float...
Please write the code in Python. Write a program/function in any Object-Oriented programming language that will...
Please write the code in Python. Write a program/function in any Object-Oriented programming language that will implement Queue Abstract Data Type with the following functions/methods.  Any build-in/pre-defined Queue function/library (e.g., java.util.Queue in Java) is NOT allowed to use in your code. push(Element):  insert the input Element (e.g., String or Integer in Java) to the end of the queue. pop(): remove the head element of the queue and print the head element on screen. count():  return the total number of elements in the queue...