BIS1003-Introduction to Programming Intensive
BIS1003 Introduction to Programming
Workshop 6
Topic 6: Function II
Apply and test your knowledge of the current and previous topics by attempting the
questions below. Completing these questions will help you to succeed in your subject.
Complete workshop-6 activities and submit your work during the workshop session.
Workshop Questions
1. This program will accept three numbers (N, A, B). You need to pass these number to
a function. This function should generate and print N random values between A and
B. The function should count how many odd values and even values generated. You
should return these counts to your program's main function and then display them to
the user.
For example, if (N=5, A=1, B=100) and if the program generates 3, 28, 13, 79 and
80, it will print out to the user odd values = 3 and even values = 2.
2. Write a python program that will accept a positive number and pass this number to a
function called "prime_function". This function takes one argument as a positive
number and returns to the calling function if the number is prime or not. You need to
print the return value from the main() function.
Hint: a prime number is a whole number greater than 1, that has only two factors one
and the number itself. A prime number is divisible only by number 1 or itself.
For example, 2, 3, 5, 7 and 11 are the first few prime numbers.
To check 3 is a prime number. Start from number 2 and check if 3 is divisible by 2
(use remainder operator: %). If not divisible by 2, move to next divisor, 3. If divisible
by 3, stop iterating and report the number as prime. However, if you have a big
number (e.g. 375), if the divisor value exceeds the number's square root, stop
iterating and print it as prime.
If a number divisible by any number except 1 and itself, stop and report the number
as non-prime.
3. Write a module called 'grade_calculator.py' which will have a function called
calculate_grades(). The function will take an input called student_mark as a
parameter and return the grade to the students. The grades are calculated based on
the total marks for the student according to the following table.
Mark Grade
0-49.9 F
50-64.9 P
65-74.9 C
75-84.9 D
85-100 HD
Now write a program called 'student.py' that will ask the user to enter the student's
total mark. Note that the 'grade_calculator.py' module should be imported into this
program. The program should then call the calculate_grades() function by passing
the student's mark and getting the grade. Finally, the program should display the
grade to the user.