Homework Assignment for 3.3-4
numbers = [0,1,2,3,4,5,6,7,8,9,10]
evens = []
for i in numbers: #this for loop is iteration because the loop goes through all the numbers in the list "numbers"
if (numbers[i] % 2 == 0): #this if statement represents selection because only the even numbers are appended to the even list.
evens.append(numbers[i])
print(evens)
#All of the code together is sequencing because sequencing is when the code flows and works together in a certain order.
numbers = [0,1,2,3,4,5,6,7,8,9,10]
odds = []
for i in numbers: #this for loop is iteration because the loop goes through all the numbers in the list "numbers"
if (numbers[i] % 2 == 1): #this if statement represents selection because only the odd numbers are appended to the odd list.
odds.append(numbers[i])
print(odds)
#All of the code together is sequencing because sequencing is when the code flows and works together in a certain order.
i = 1
starString = "*"
while i <= 5:
j = 1
while j <= i:
print ("*", end= "")
j += 1
print ()
i += 1
3.3 Video 2 Hacks
Practice Problems
- 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
☆ My answer: First, a is set to the value of b which is 1. Next, b is set to the value of c + d = 3 + 4 = 7. Lastly, d is set to the value of b which changed to 7. With this, we know that a = 1, b = 7, c = 3, d = 7.
Click for the answer!
a = 1, b = 7, c = 3, d = 7- 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?
- the value of hot is true, the value of cold is true
- the value of hot is false, the value of cold is true
- the value of hot is true, the value of cold is false
- the value of hot is false, the value of cold is false
☆ My answer: First, cold is set to hot with is true. Next, hot is set to cold which has a value of true. This means that Both variables will have values of true. This means the correct answer is #1.
Click for the answer!
1. the value of hot is true, the value of cold is true- 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.
Making My Own Code Segements:
First Code Segment:
num1 ⟵ 12
num2 ⟵ 2
num3 ⟵ 14
num4 ⟵ 0
num3 ⟵ num1
num4 ⟵ num1 + num2
DISPLAY(num1)
DISPLAY(num2)
DISPLAY(num3)
DISPLAY(num4)
What is displayed by this code segment?
☆ My answer: First, num3 is set to the value of num1 which is 12. Next, num4 is set to the sum of num1 + num2 which is 14. Therefore, this code segment should display: 12 2 12 14.
Second Code Segment:
first ⟵ true
second ⟵ false
third ⟵ true
second ⟵ third
third ⟵ first
DISPLAY(first)
DISPLAY(second)
DISPLAY(third)
What is displayed by this code segment?
☆ My answer: First, second is set to the value of third which is true. Next, third is set to the value of first which is true. This means that the code segment should display: true true true.
- Sequencing
num1 = 3
num2 = 1
num3 = 5
num1 = num2 + num3
num2 = num1 + num3 # num2 is now the new num1 + num3
What is the value of num1 and num2?
☆ My answer: The value of num1 is 6 because it's the sum of num2 and num3 which is 1 + 5. The value of num2 is 11 because it's the sum of num1 and num3 which is 6 + 5.
Click for the answer!
num1 = 6, num2 = 113.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?
☆ My answer: The result would be "SmithB@gmail.com".
Hint: var = "B" name = "SmithB"
-
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)
☆ My answer: This code segment will display "ompuook". ("ompu" from "computer" and "ook" from "textbooks")
Answers
Test 1
-
Result: "SmithB@gmail.com"
Test 2
- Result: "ompuook"