Application Exercise 3
Write, save, and run a Python program, that will do the following when run: YOU MAY
CREATE INDIVIDUAL PROGRAMS FOR EACH OF THE FOLLOWING TASKS.
- Make sure to name the files: app3-part1.py and app3-part2. Submitting in other
formats will be penalized or not marked.
- Follow instructions given, EXACTLY !
- Make sure to follow the proper naming convention inside your program when
naming variables.
- COPYING from others will be heavily penalized and REPORTED to the Dean of
the School.
- Late submissions are marked ZERO.
PART I (5 marks)
Using functions, write a program that will:
1. Ask the user how many floating point numbers with 5
integer digits and two decimal digits the program should accept.
That means you need to check if the number entered by the
user has 5 digits and two decimal digits to be accepted. Your
program should accept five of them.
2. Add them the numbers and display a subtotal.
count = 0
sum=0
while count<5:
num1 = float(input("Enter number : "))
ref=99999
ref1_f = 0.99
n = float(format(num1, '1.0f'))
print(n)
y=num1-n
z=float(format(y,'0.2f'))
print(z)
if 0<n<1000000:
if 0.0<z<0.99:
sum = num1+sum
print("number is accepted")
count = count + 1
else:
print("enter the number again")
count = count
else:
print("enter the number again")
count = count
print(sum)
PART II (5 marks)
1. Create a function that prints your company name, your 1 mark
position and your gross income each in a new line.
Solution
def greet(company_name,position,salary):
print(company_name)
print(position)
print(salary)
greet("Dominos","Manager","25000")
2. Create a function that prints your company name, your 1 mark
position and your gross income each in a new line. The
function has three parameters. Now, in your program
create three variables and assign values to them. Call
the function and pass the required arguments.
Solution
def greet(company_name,position,salary):
print(company_name)
print(position)
print(salary)
company_name="Dominos"
position="Manager"
salary="25000"
greet(company_name,position,salary)
3. Create a function that multiplies two numbers then 1.5 mark
calculates the square root of the result, and then returns
the result to the caller. In your program, first, ask the
user to enter two numbers. Then call the function. The
caller passes to the function the two numbers as
arguments, then the program prints the returned result.
import math
def add_num(a,b):#function for multiplication
square_root=math.sqrt((a*b));
return square_root; #return value
num1=int(input("input the number one: "))
num2=int(input("input the number one: "))
print("The product is",add_num(num1,num2))
4. Create a main function in which you call two functions: 1 mark
the first one calculates the power of a given number (a)
and then the function prints the result.
The second function calculates the modf of a given
number( theModF = modf (y) , where y is the given
number) and then the function prints the result.
The main function contains all variables assignments. In
the main function, these variables are passed as
arguments when calling the two functions.
Answer
import math
def pow_num(a):#function for multiplication
power=(a*a);
return power; #return value
def mod_num(y):
num1=int(input("input the number one: "))#input from
user for num1
modulus=pow_num(num1)%y
return mod_num;
y=int(input("input the number one: "))#input from user
for num1
print("The product is",mod_num(y))
5. The output of the program should look similar to the 0.5 mark
output of a sample run shown below.
Answer
import math
def pow_num(a):#function for multiplication
power=(a*a);
return power; #return value
def mod_num(y):
num1=int(input("input the number one: "))#input
from user for num1
modulus=pow_num(num1)%y
return mod_num;
y=int(input("input the number one: "))#input from
user for num1
print("The product is",mod_num(y))
5 marks total
#Sample run
The ABC company
Software Engineer
85,000
The ABC company
Software Engineer
85,000
Please, enter the first number: 9
Please, enter the second number: 4
The result of the square root is : 6
The power is : (any result)
The value of modf is : (any result)