Functions and parameter passing

The purpose of this assignment is to assess your understanding of
• User defined functions and parameter passing (Section 3.3 and 3.5)
• List methods (Section 2.3 pages 31-33)
• String formatting (Section 4.1 and 4.2)
• Iteration Structure – for loop (Section 3.2)
• Reading files (Section 4.3)
SUBMISSION
• Include your full name as a comment in the first line of your Python program
• Include the problem number as a comment in the second line of our Python program
• Save each program to a separate file labeled as YourName_hw3_1.py, YourName_hw3_2.py,
YourName_hw3_3.py accordingly
• Upload each file to Submissions folder in D2L.
PROBLEMS
Note: you may not use Python statements, functions, data types, etc. that where not discussed in
the reading assignment or the lecture notes/videos for week 3 or previous weeks. This is a class
for students who have not programmed before and I expect everyone to code on the same level. If
you have a better way of writing the code, then upload two versions: one that codes according to
the specifications and the other that demonstrates advanced programming techniques.
I encourage you to use computational thinking to solve the problems. These are straight-forward
solutions, but developing a good habit of analyzing the problem and describing the steps will serve
you well as the problems get more complex.
I encourage you to test your code thoroughly, but I am not requiring that you include you test cases
as part of your programs. I do require that you code according to the problem specifications.
PROBLEM ONE
Write a Python program that separates a list of up to 100 positive and negative integers
into a sorted list of even integers and a sorted list of odd integers. There are two userdefined functions in this program.
2
PART ONE (10 POINT)
USER-DEFINED FUNCTION, LIST, ITERATION (FOR LOOP), DECISION
Write the function evenOdd (num) that has 1 argument, num, a positive integer no
greater than 100. This function returns a list of sorted even integers and a list of
sorted odd integers.
• Create a list, lst, of positive and negative integers using the following code:
import random # include outside of function
for i in range(num):
lst.append(random.randint(-50, 50))
lst will contain num positive and negative integers between -50 and 50. There may
be duplicate numbers.
• Divide lst into 2 lists; One that contains all the even integers and one that contains
all the odd integers.
• Even integers are divisible by 2; odd integers are not divisible by 2.
• The function returns the two lists. Each list is sorted in ascending order. (use list
sort method)
PART TWO (10 POINTS)
USER-DEFINED FUNCTION, LIST, ITERATION (FOR LOOP), DECISION, FORMATTING
Write the function main( ) that has no arguments.
• main() has no arguments
• Ask the user to enter the number of integers they want in the list. The
number may not be greater than 100.
• Call the evenOdd() function passing the number the user entered to the
function
• Evaluate the returned results and displays the list in two formatted
columns; one column of even numbers and another column of odd numbers
• You must pass arguments from the calling program function to the called
program function. (you may not use global variables i.e. variables defined
outside of either function.)
3
Sample cases:
PROBLEM 2 (10 POINTS)
ITERATION (FOR LOOP), FORMATTING
The monthly payments for a given loan are divided into amounts that apply to the principal
and to the interest. For example: if you make a monthly payment of $500, only a portion
of the $500 goes to the principal and the remainder is the interest payment.
• monthly interest = monthly interest rate x balance of principal
4
• balance of principal = monthly payment – monthly interest
• monthly payment =
(monthly interest rate x balance of principal) / (1 – (1 + monthly interest rate) –months)
Write a Python program that accepts input from a user for a loan amount (principal), annual interest
rate (convert to monthly interest rate), and loan period (in number of year, convert to number of
months) and displays a table with six columns: payment number, monthly payment, interest, principal,
remaining balance after the payment, and the total interest paid to name.
For example: a loan payment schedule for a 1-year (12 months) loan of $5000 with a 12% annual
interest rate (.01 monthly interest rate) is:
Test your code with the sample data then test again with the loan period of half a year, i.e. .5 years.
5
PROBLEM THREE (10 POINTS)
READ FILE, LIST, DECISION
Write a function wordGame() that reads the text file, Pride_and_Prejudice.txt. (Be sure
to save Pride_and_Prejudice.txt in the same folder in which you are saving your program.)
Include code in this function that retrieves 2 random words from the file. Ask the user to
guess which of the words the author used more often in the text. If the user picks the
word with the higher count, then they have guessed correctly.
To pick a random word, import random and use the random.choice () method. To determine
how often a word occurs (use the count method). Use a logical condition to determine if
the user guessed correctly. As shown in the sample cases, be sure to include the chosen
words in double quotes.
Note: There is only one function in this program. To run the function, type wordGame() at
the IDLE prompt.
Sample cases :
Assignment Three Grading Rubric
Learning outcomes:
• Create user-defined functions
• Utilize string methods in text processing
• Read text file from within a Python program
• Use the String method format() to improve the readability of the output string
• Use iteration structure to repeat code
• Use methods from random library