3.3 Video 1 Hacks

Show two examples and label which one is sequence, selection, iteration

numbers = [0,1,2,3,4,5,6,7,8,9,10]
evens = []

for i in numbers:
    if (numbers[i] % 2 == 0):
        evens.append(numbers[i])

print(evens)
[0, 2, 4, 6, 8, 10]

Answers 1

All the steps combined are sequencing

The step "for i in numbers:" is iteration because they go through all the numbers.

"if (numbers[i] % 2 == 0)" is selection because they sort each number to find the even ones.

i = 1
starString = "*"
while i <= 5:
  j = 1
  while j <= i:
    print ("*", end= "")
    j += 1
  print ()
  i += 1
*
**
***
****
*****

Answers 2

All the steps are a sequence

"While i <= 5:" is iteration because they repeat until i reaches 5

"While j <= i:" is selection because this is where they decide what j is

3.3 Video 2 Hacks

Practice Problems

  1. given the following code segment below:

a ⟵ 7

b ⟵ 1

c ⟵ 3

d ⟵ 4

a ⟵ b

b ⟵ c + d

d ⟵ b

find the value for a, b, c, d A = 1 since it becomes B B = 7 since its 4+3 C = 3 since its unchanges

Click for the answer! a = 1, b = 7, c = 3, d = 7
  1. consider the following code segment:

hot ⟵ true

cold ⟵ false

cold ⟵ hot

hot ⟵ cold

what are the values of hot and cold after executing the code segment? 1. the value of hot is true, the value of cold is true

  1. the value of hot is false, the value of cold is true
  2. the value of hot is true, the value of cold is false
  3. the value of hot is false, the value of cold is false

    Click for the answer!</p>
     1. the value of hot is true, the value of cold is true 
    

    </details> THIS Iis becuase if you follow it hot = cold which = true and cold = hot which equals true

    </li>
  4. Make TWO of your own code segments that contain at least 5 defined variables, then provide the answer and EXPLAIN why your answer is correct.

  5. </ol>

    a ⟵ 1

    b ⟵ 2

    c ⟵ 3

    d ⟵ 4

    a ⟵ b

    what is the value of A?

    1. 2

    1. 5
    2. 8

    a ⟵ 4

    b ⟵ 3

    c ⟵ 2

    d ⟵ 1

    d ⟵ c

    What is the value of C

    1. 2
    2. 4 3. 1
    </div> </div> </div>
    1. Sequencing
    num1 = 3
    num2 = 1
    num3 = 5
    num1 = num2 + num3      
    num2 = num1 + num3      # num2 is now the new num1 + num3
    
    # number 1 is 6 as you add 5 + 1 making it 6.
    # number 2 is 11 becuase its added up within the numbers
    num 1 = 6 
    num 2 = 11
    

    What is the value of num1 and num2?

    Click for the answer! num1 = 6, num2 = 11

    3.3 Video 3 Hacks

    3.4 Video 1 Hacks

    String Homework


    • Test 1

      firstName <- "Bob" lastName <- "Smith" var <- substring(firstName, 1, 1) name <- concat(lastName, var) email <- concat(name, "@gmail.com") DISPLAY(email)

    • What would the result be?

      Hint: var = "B" name = "SmithB"

      Answer SmithB@gmail.com Explanation: This is becuase the and the name is smithb and the email is the name with gmail.com


    • Test 2

      word1 <- "computer" word2 <- "textbooks" length1 <- len(word1)/2 length2 <- len(word2)/3 first <- substring(word1, 2, len1) second <- substring(word2, len2+3, len2) newWord <- concat(first, second) DISPLAY(newWord) <mark

    Answer ompuook

    Explanation after using the len which is so you get 3, 2 and ompu after you get ook which gives you the answer as concat adds the words together. </mark>
    

    Answers

    Test 1

    • Result:"SmithB@gmail.com" Test 2
    • Result: "ompuook"
    </div>