Hacks Unit 3 Section 3.8.1

  1. Define an Iteration
  2. Make your own example of an iteration with at least 4 steps and a stopping condition(Similar to mine that I did)
  3. Program a simple iteration.
  • Iteration: loop that continues repeating until condition is met
  • Example of iteration with stopping condition:
    • I am doing an easter egg hunt and trying to frind the golden egg. I have split the searching area into sections
    1. I walk to a section
    2. I look aroung to try and find the golden egg
    3. If it is not there, I add one to the section number (move onto next section)
    4. I repeat steps 1 through 4
    5. Once I find the egg, I stop searching to go brag to the kids
hourshomework = 0
while hourshomework < 8:
    print("Too easy")
    hourshomework += 1
    if hourshomework == 6:
        break
Too easy
Too easy
Too easy
Too easy
Too easy
Too easy

Hacks Unit 3 Section 3.8.2

  1. What is an iteration statement, in your own words?
  2. Create a descending list of numbers using for loop
  3. Using while loop, make a list of numbers which will form an output of 3,16,29,42,55,68,81
  • Iteration statement: a condition that must be met in a loop or it will continue repeating
numlist = [50]
for i in numlist:
    i -= 1
    numlist.append(i)
    if i == 0:
        break
print(numlist)
[50, 49, 48, 47, 46, 45, 44, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33, 32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
i = 3
print(i)
while i <= 68:
    i += 13
    print(i)
3
16
29
42
55
68
81

Find the lowest value in a list

  • 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"]
min = 20
for n in nums:
    if int(n) < int(min):
        min = n
print(min)
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 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! 
monthly_earnings = []

a = 0
while a == 0:
    money = input("How much money did you make this month?")
    if money == "":
        break
    monthly_earnings.append(int(money))

sort = input("Do you want to sort your earnings?")

if sort == "yes":
    print("sorted:")
    print(sorted(monthly_earnings))
else:
    print("unsorted:")
    print(monthly_earnings)
print("Your highest monthly earning is $" + str(max(monthly_earnings)))
print("You have been working for " + str(len(monthly_earnings)) + " months")
sorted:
[2, 36, 345, 578, 9000]
Your highest monthly earning is $9000
You have been working for 5 months