Unit 3.8

Iteration: a repeating portion of an algorithm until a given condition is met

Iteration Statements: change the sequential flow of control by repeating a set of statements zero or more times, until a stopping condition is met Repeat Until: if the condition evaluates to true if a previous condition is true

Online shopping example:

image

i=1
while i<=5:
	print(i)
	i=i+1
1
2
3
4
5

Unit 3.8.1

  1. An iteration is a repeating part of an algorithm that repeats through a specified number of times until an condition is met
  2. Example of an iteration: iterating through number of tasks until they are done

photo

Make your own example of an iteration with at least 4 steps and a stopping condition(Similar to mine that I did)

  1. Program a simple iteration.
names = ["Grace", "John", "Mort", "Claire", "Ronia", "Nick"]

for name in names:
    print("Hi " + name + '!')
Hi Grace!
Hi John!
Hi Mort!
Hi Claire!
Hi Ronia!
Hi Nick!

Unit 3.8.2

  1. iteration statement will execute a code as long as the a condition is True

iterative statements are used to execute a part of the program repeatedly as long as a given condition is True. What is an iteration statement, in your own words? Create a descending list of numbers using for loop Using while loop, make a list of numbers which will form an output of 3,16,29,42,55,68,81

for number in range(20, 0, -1):
    print(number)
20
19
18
17
16
15
14
13
12
11
10
9
8
7
6
5
4
3
2
1
i = 0
number = 3
while i < 7:
    print(number)
    number += 13
    i += 1
3
16
29
42
55
68
81

Unit 3.10

Find the lowest value in a list Use the list made below Make a variable to hold the minimum and set it to potential minimum value Loop Check each element to see if it is less than the minimum variable If the element is less than the minimum variable, update the minimum After all the elements of the list have been checked, display the minimum value

nums = [38, 45, 67, 83, 78]

minimum = 100

for number in nums:
    if number < minimum:
        minimum = number

print(minimum)
38

AP College Board Reference Sheet

#assigns a list to a variable
x <- aList 

#assigns value of aList[i] to aList[j]
aList[i] <- aList[j]

#any values in aList at indecies >= i are shifted one position to the right. Length of list is increased by 1, value is plaed at index i in aList
insert(aList, i, value)

#appends aList in increased by 1, and value placed at end of aList
append(aList, value)
import getpass, sys
import random

def ask_question (question, answer):

    print(question)
    ans = input(question)
    print(ans)
   
    if ans == answer:
        print("Correct!")
        return 1

    else:
        print("Wrong")
        return 0

question_list = ["What allows a value to be inserted into a list at index i?" , "What allows an element at index i to be deleted from a list?" , "What returns the number of elements currently in a specific list?" , "What allows a value to be added at the end of a list?"]
answer_list = ["index()", "remove()", "length()" , "append()"]

# Set points to 0 at the start of the quiz
points = 0

# If the length of the quiz is greater than 0, then random questions will be chosen from the "question_list" set
while len(question_list) > 0:
    index = random.randint(0, len(question_list) - 1)
    
# The points system where a point is rewarded for each correct answer    
    points = points + ask_question(question_list[index], answer_list[index])
    
# If a question or answer has already been used, then it shall be deleted    
    del question_list[index]
    del answer_list[index]

# Calculating score using the points system and dividing it by the total number of questions (6)
score = (points / 4)

# Calculating the percentage of correct answers by multiplying the score by 100
percent = (score * 100)

# Printing the percentage, and formatting the percentage in a way where two decimals can be shown (through "{:.2f}")
print("{:.2f}".format(percent) + "%")

# Adding final remarks based upon the users given scores
if points >= 5:
         print("Your total score is: ", points, "out of 4. Amazing job!")

elif points == 4:
         print("Your total score is: ", points, "out of 4. Not too bad, keep on studying! " )

else:
         print("Your total score is: ", points, "out of 4. Its alright, better luck next time!")
What allows a value to be added at the end of a list?
append()
Correct!
What allows a value to be inserted into a list at index i?
index()
Correct!
What allows an element at index i to be deleted from a list?
remove()
Correct!
What returns the number of elements currently in a specific list?
length()
Correct!
100.00%
Your total score is:  4 out of 4. Not too bad, keep on studying!