CS计算机代考程序代写 python Digression: Scoring Matrices

Digression: Scoring Matrices

Lecture 5
Definite loops

L5 Difinite Loops – 2

Objectives of this Lecture

• To understand simple definite loops

• Follow the software development process to develop
a program that calculates the future value of
investment

L5 Difinite Loops – 3

Definite Loops
• A definite loop executes a pre-specified number of

times, iterations, which is known when program is
loaded

• for in :

• The beginning and end of the body are indicated by
indentation.

• Note that iterations are over sequences (more of this
in a later lecture)
– For the time being, a sequence is a countable

sequence of Python things (objects)

L5 Difinite Loops – 4

Definite Loops

for loops alter the flow
of program execution,
so they are referred to
as control structures.

L5 Difinite Loops – 5

Definite Loops

• for in :

• The variable after the for is called the loop index. It
takes on each successive value in sequence

L5 Difinite Loops – 6

Definite Loops
>>> for item in [1,2,3,4,6]:

print(item)
1
2
3
4
6

Loop variable item first has the value 1, then 2, then 3,
then 4 and finally 6 (there is no 5 in the sequence)

L5 Difinite Loops – 7

Definite Loops
• In chaos.py (from Labsheet 00), what did
range(10) do?

>>> list(range(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

• range is a built-in Python function that generates a
sequence of numbers, starting with 0.

• list is a built-in Python function that turns the
sequence into an explicit list

• The body of the loop executes 10 times

L5 Difinite Loops – 8

Definite Loops
>>> for num in range(10)

print(num)
0
1
2
3
4
5
6
7
8
9

L5 Difinite Loops – 9

Definite Loops
>>> for odd in [1, 3, 5, 7]:

print(odd*odd)
1
9
25
49

Loop variable odd first has the value 1, then 3, then 5
and finally 7

L5 Difinite Loops – 10

Example Program: Interest Earned

Analysis

– Money deposited in a bank account earns
interest.

– How much will the account be worth 10 years
from now?

– Inputs: principal amount, interest rate

– Outputs: value of the investment in 10 years

L5 Difinite Loops – 11

Example Program: Future Value
Specification
• Inputs

principal The amount of money being invested, in dollars
apr The annual percentage rate expressed as a floating point

decimal number 0.0 < apr < 1.0 • Output The value of the investment 10 years in the future • Relationship Value after one year is given by principal * (1 + apr). This needs to be done 10 times. L5 Difinite Loops - 12 Example Program: Future Value Design – Print an introduction – Input the amount of the principal (principal) – Input the annual percentage rate (apr) – Repeat 10 times: principal = principal * (1 + apr) – Output the value of principal Computational thinking: Decomposition – dividing the problem in small tasks L5 Difinite Loops - 13 Example Program: Future Value Implementation – Each line translates to one line of Python (in this case) – Print an introduction print("This program calculates the future") print("value of a 10-year investment.") – Input the amount of the principal principal = float(input("Enter the initial principal: ")) L5 Difinite Loops - 14 Example Program: Future Value – Input the annual percentage rate apr = float(input("Enter the annual interest rate: ")) – Repeat 10 times: for i in range(10): – Calculate principal = principal * (1 + apr) principal *= (1 + apr) – Output the value of the principal at the end of 10 years print("The value in 10 years is:", principal) L5 Difinite Loops - 15 Example Program: futval.py # A program to compute the value of an investment # carried 10 years into the future # Author: Unit Coordinator def main(): print("This program calculates the future") print("value of a 10-year investment.") principal = float(input("Enter the initial principal: ")) apr = float(input("Enter the annual interest rate: ")) for i in range(10): principal *= (1 + apr) print ("The value in 10 years is:", principal) main() L5 Difinite Loops - 16 Example Program: Testing futval.py >>> main()

This program calculates the future

value of a 10-year investment.

Enter the initial principal: 100

Enter the annual interest rate: 0.03

The value in 10 years is: 134.391637934

>>> main()

This program calculates the future

value of a 10-year investment.

Enter the initial principal: 100

Enter the annual interest rate: 0.10

The value in 10 years is: 259.37424601

L5 Difinite Loops – 17

Example Program: Interest Earned
(generalized)

Analysis

– Money deposited in a bank account earns
interest.

– How much will the account be worth after input
years from now?

– Inputs: principal amount, interest rate, years

– Outputs: value of the investment after input
years

L5 Difinite Loops – 18

Example Program: futval_gen.py
# A program to compute the value of an investment

# after specific number of years

# Author: Unit Coordinator

def main():
print(“This program calculates the future”)
print(“value for the investment after number of years.”)

principal = float(input(“Enter the initial principal: “))
apr = float(input(“Enter the annual interest rate: “))
yrs = int(input(“Enter number of years: “))

for i in range(yrs):
principal *= (1 + apr)

print (“The value in”, yrs, “years is:”, principal)

main()

L5 Difinite Loops – 19

Example Program: Testing futval_gen.py
>>> main()

This program calculates the future

value for the investment after number of years.

Enter the initial principal: 100

Enter the annual interest rate: 0.03

Enter number of years: 10

The value in 10 years is: 134.391637934

>>> main()

This program calculates the future

value for the investment after number of years.

Enter the initial principal: 150

Enter the annual interest rate: 0.12

Enter number of years: 25

The value in 10 years is: 2550.009660996408

L5 Difinite Loops – 20

Lecture Summary

• Definite loop:
– “for” loop alters the sequence of the program
– Runs equal to number of items in the sequence
– The loop index takes a value of sequence for each

iteration
• Future value calculation for the investment

Lecture 5�Definite loops
Objectives of this Lecture
Definite Loops
Definite Loops
Definite Loops
Definite Loops
Definite Loops
Definite Loops
Definite Loops
Example Program: Interest Earned
Example Program: Future Value
Example Program: Future Value
Example Program: Future Value
Example Program: Future Value
Example Program: futval.py
Example Program: Testing futval.py
Example Program: Interest Earned (generalized)
Example Program: futval_gen.py
Example Program: Testing futval_gen.py
Lecture Summary