Hacks Unit 3 Section 3.8.1

Define an Iteration

  • An iteration repeats over an algorithm until a certain condition is met

Make your own example of an iteration with at least 4 steps and a stopping condition(Similar to mine that I did) Homework loop Check physics homework: if there is homework: sit down and do physics if there is calc homework sit down and do calc homework if there is csp hw sit down and do cs if there is apel hw sit down and do cs if there are upcoming tests study for the subject

numbers = [1,4,5,7,8,9,10]
newnum =[]
for i in numbers:
    num = i*7
    newnum.append(num+6)
    if num ==49:
        break
    print(newnum)
[13]
[13, 34]
[13, 34, 41]

Program a simple iteration.

names = ["alex", "bob", "billy", "Joe"]
for i in names:
    print(i)
alex
bob
billy
Joe

Find the lowest value in a list (Luna Iwazaki) Use the list made bellow 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 = ["10", "15", "20", "25", "30", "35"]
minimum = min(nums)
print(minimum, "This is the lowest number")
low = int(nums[0])
for i in range(len(nums)):
    if int(nums[i]) < low:
        low = nums[i]
print("the lowest number in nums is:", low)
#good
10 This is the lowest number
the lowest number in nums is: 10
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 returns the number of elements currently in a specific list?
length()
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!
100.00%
Your total score is:  4 out of 4. Not too bad, keep on studying!