Section 3-4
Lesson on Big Idea 3 which includes expressions, strings, psuedocode, and more!
Sequencing Practice: the code below does not follow the intended steps below. change the code so that it does so.
- divide value1 by 10(value1 = 5)
- multiply 2 from the result of the step 1
- subtract 4 from the result of the step 2
- print the result of step 3
value1 = 5
value2 = value1 / 10 #step 1
value3 = value2 * 2 #step 2
value4 = value3 - 4 #step 3
print(value4)
Selection/Iteration Practice: Create a function to print ONLY the numbers of numlist that are divisble by 3.
Hint: use the MOD operator (a % b) to find the remainder when a is divided by b.
numlist = [3,4,9,76,891]
for x in numlist:
if x % 3 == 0:
print(str(x) + " is divisible by 3")
continue
else:
print(str(x) + " is not divisible by 3")
continue
Homework/Binary Adaptation: Create a python function that will convert a decimal number 1-255 to binary using mathematical operations and powers of 2. Challenge: add frontend with javascript or html.
nums = []
x = int(input("Enter a number between 1 and 255:"))
def convert(x):
if x >= 1 and x <= 255:
while x/2 != 0:
rem = x % 2
nums.append(rem)
x = int(x/2)
nums.reverse()
binary = " "
for i in nums:
binary = binary + str(i)
return binary
else:
print("Number not within range.")
print(str(x) + " to binary: " + convert(x))
What is psuedocode?
Pseudocode is writing out a program in plain language with keywords that are used to refer to common coding concepts.
Can you think of some benefits of using pseudocode prior to writing out the actual code?
- Choose an everyday activity
- Imagine that you are providing instructions for this activity to a person who has never done it before
- Challenge someone to do the steps you wrote out
Ex. Brushing Teeth
- Pick up your toothbrush
- Rinse toothbrush
- Pick up toothpaste
- Place toothpaste on the toothbrush
- Rinse toothbrush again
- Brush teeth in a circular motion
- Spit
- Wash mouth
- Rinse toothbrush
- You have brushed your teeth!
#the substring will have the characters including the index "start" to the character BEFORE the index "end"
#len(string) will print the length of string
word = "hellobye"
x = word[0:5]
y = word[5:]
print(x)
print(y)
print("Total length: " + str(len(word)) + " letters")
Concatenation Practice: combine string1 and string2 to make string3, then print string3.
string1 = "computer"
string2 = "science"
string3 = string1 + " " + string2
print(string3)
Homework/List Adaptation: create a function that prints the name of each string in the list and the string's length. Challenge: add frontend with javascript or html.
names = ["jaden","max","dylan","orlando"]
def length(names):
for x in names:
print("Length of " + x + ": " + str(len(x)))
length(names)
your_name = input("What's your name?:")
print("Your name is " + str(len(your_name)) + " letters long.")
vowels = 0
cons = 0
for l in your_name:
if l == "a" or l == "e" or l == "i" or l == "o" or l =="u":
vowels += 1
else:
cons += 1
print("Your name has " + str(vowels) + " vowels.")
print("Your name has " + str(cons) + " consonants.")
Stuck?
- Check out what we did.