Pseudocode Operation Python Syntax Description
aList[i] aList[i] Accesses the element of aList at index i
x ← aList[i] x = aList[i] Assigns the element of aList at index i
to a variable 'x'
aList[i] ← x aList(i) = x Assigns the value of a variable 'x' to
the element of a List at index i
aList[i] ← aList[j] aList[i] = aList[j] Assigns value of aList[j] to aList[i]
INSERT(aList, i, value) aList.insert(i, value) value is placed at index i in aList. Any
element at an index greater than i will shift
one position to the right.
APPEND(aList, value) aList.append(value) value is added as an element to the end
of aList and length of aList is increased by
1
REMOVE(aList, i) aList.pop(i)
OR
aList.remove(value)
Removes item at index i and any values at
indices greater than i shift to the left.
Length of aList decreased by 1.

Notes

  • Lists are collections of data
  • Lists are very helpful because you can use them to store unlimited amounts of data
  • Using loops and functions that locate list data using indexes, you can perform specific functions on lists
  • List indexes generally start with 0
  • Numbers in list can be used for math
  • Iteration is basically repetition
  • It is helpful to have code that repeat a function so that you don't have to copy-and-paste over and over again
  • A function generally iterates a certain number of times based on its purpose
  • For loops check the information stored within a list and use it as specified by the given variable
  • For loops are good for applying a certain alogrithm or function to an entire list of similar things
  • You can also use dictionaries to perform functions and make your code neater and more organized
  • Recursive loops involve incrementing a variable until it reaches a certain break point
  • While loops are very similar to recursive loops, but with a different syntax.

Homework

Instead of us making a quiz for you to take, we would like YOU to make a quiz about the material we reviewed.

We would like you to input questions into a list, and use some sort of iterative system to print the questions, detect an input, and determine if you answered correctly. There should be at least five questions, each with at least three possible answers.

You may use the template below as a framework for this assignment.

import random

def Quiz(prompt, choices):
    global word
    print("Question: " + prompt)
    for c in choices:
        print(c)
    
    word = input()
    return word


questions = [
    {"question":"What is the type of loop covered in the lesson?", "correct_answer":"for loop", "incorrect_answer":["when loop", "do loop"]},
    {"question":"What number do most coding language indexes start with?", "correct_answer":"0", "incorrect_answer":["1", "-1"]},
    {"question":"How can you add a value into a list?", "correct_answer":"append()", "incorrect_answer":["index()", "remove()"]},
    {"question":"How can you remove a value into a list?", "correct_answer":"pop()", "incorrect_answer":["insert()", "int()"]},
    {"question":"What is it called when you repeat a function over and over?", "correct_answer":"iteration", "incorrect_answer":["dictionary", "repetition"]}]
    

correct_answers = 0
#for i in questions: 
#    print (i.items())
#    for n in i:
#        print(n)
print("Hello, you will be asked 5 short multiple chocie questions.")
for i in range(0, len(questions)):
    multiple_choices = []
    multiple_choices.append(questions[i]["correct_answer"])
    multiple_choices.append(questions[i]["incorrect_answer"][0])
    multiple_choices.append(questions[i]["incorrect_answer"][1])
    random.shuffle(multiple_choices)
    Quiz(questions[i]["question"], multiple_choices)
    if word.lower() == questions[i]["correct_answer"]:
        print("Answer is correct")
        correct_answers += 1
    else:
        print("Answer is incorrect")
print("Your score is: " + str(correct_answers) + "/5" + " and you got " + str((correct_answers/5)*100) + "%")
Hello, you will be asked 5 short multiple chocie questions.
Question: What is the type of loop covered in the lesson?
for loop
when loop
do loop
Answer is correct
Question: What number do most coding language indexes start with?
0
1
-1
Answer is correct
Question: How can you add a value into a list?
append()
remove()
index()
Answer is incorrect
Question: How can you remove a value into a list?
insert()
pop()
int()
Answer is incorrect
Question: What is it called when you repeat a function over and over?
dictionary
iteration
repetition
Answer is incorrect
Your score is: 2/5 and you got 40.0%

Hacks

Here are some ideas of things you can do to make your program even cooler. Doing these will raise your grade if done correctly.

  • Add more than five questions with more than three answer choices
  • Randomize the order in which questions/answers are output
  • At the end, display the user's score and determine whether or not they passed

Challenges

grocery_list = ['apples', 'milk', 'oranges', 'carrots', 'cucumbers']

# Print the fourth item in the list
print(grocery_list[3])

# Now, assign the fourth item in the list to a variable, x and then print the variable
x = grocery_list[3]
print(x)

# Add these two items at the end of the list : umbrellas and artichokes
grocery_list.append("umbrellas")
grocery_list.append("artichokes")

# Insert the item eggs as the third item of the list 
grocery_list.insert(2, "eggs")

# Remove milk from the list 
grocery_list.remove("milk")

# Assign the element at the end of the list to index 2. Print index 2 to check
grocery_list[2] = "artichokes"
print(grocery_list[2])

# Print the entire list, does it match ours ? 
print(grocery_list)

# Expected output
# carrots
# carrots
# artichokes
# ['apples', 'eggs', 'artichokes', 'carrots', 'cucumbers', 'umbrellas', 'artichokes']
carrots
carrots
artichokes
['apples', 'eggs', 'artichokes', 'carrots', 'cucumbers', 'umbrellas', 'artichokes']
binarylist = [
    "01001001", "10101010", "10010110", "00110111", "11101100", "11010001", "10000001"
]

newlist = []
finallist=[]
for i in binarylist:
    newlist.append(int(i, 2))
for number in newlist:
    if number < 100:
        finallist.append(number)
print(finallist)
[73, 55]