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
1. Design a function called sum that will take as arguments two floating-point numbers and will...
1. Design a function called sum that will take as arguments two floating-point numbers and will return the sum of those two numbers. 2. Design a function called get_longer that will take as arguments two phrases (strings) and will return the longer of the two strings. 3. Design a function called get_hypotenuse that will take as arguments the two lengths of the short sides (a and b in the formula below) of a right-angle triangle and returns the length of...
3. For this problem, you should describe the algorithm using a flowchart and then write Python...
3. For this problem, you should describe the algorithm using a flowchart and then write Python code to implement the algorithm. You will write the Python code in a Python program file and then run it to obtain the output. Include in your HW document a photo of your flowchart and a screenshot of your code and the output in IDLE you get when you run the program. Algorithm: Ask the user to enter the user’s first name, the number...
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)
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...
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
Question 1 Write functions that do the following: i) A function that takes 2 arguments and...
Question 1 Write functions that do the following: i) A function that takes 2 arguments and adds them. The result returned is the sum of the parameters. ii) A function that takes 2 arguments and returns the difference, iii) A function that calls both functions in i) and ii) and prints the product of the values returned by both. Question 2 Write functions: i) One that prompts a user for 2 numbers. ii) Adds the two numbers if they are...
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...
Create a Python main program which calls two functions enterNum and calcResult and accomplishes the following:...
Create a Python main program which calls two functions enterNum and calcResult and accomplishes the following: 1. The main program calls the function enterNum 3 times. The first time enterNum is called, the main program stores the returned output in the variable xx. The second time, the returned output is stored in the variable yy and the third time in zz. 2. enterNum asks the user to enter a floating point number. This function has no input arguments and returns...
Module 4 Assignment 1: Pseudocode & Python with Variable Scope Overview Module 4 Assignment 1 features...
Module 4 Assignment 1: Pseudocode & Python with Variable Scope Overview Module 4 Assignment 1 features the design of a calculator program using pseudocode and a Python program that uses a list and functions with variable scope to add, subtract, multiply, and divide. Open the M4Lab1ii.py program in IDLE and save it as M4Lab1ii.py with your initials instead of ii. Each lab asks you to write pseudocode that plans the program’s logic before you write the program in Python and...
using python Write a def function “amc” with a positive integer “n” input parameter. It returns...
using python Write a def function “amc” with a positive integer “n” input parameter. It returns the smallest amicable number greater than “n”. Two different numbers are both amicable if the sum of the proper divisors of each is equal to the other. Any number that's part of such a pair is an amicable number. Hint: You may want to create a separate function to sum proper divisors. def amc(n): Return the smallest amicable number greater than positive integer n....
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT