P4-M 4/24 Big Idea 3
Lesson about Big Idea 3
Introduction: Zeen
Hello, my name is zeen and today we will be presenting big idea 3. Our topics include 2d arrays, iteration, and lists and dictionaries.
Objectives
Master the concepts of iteration, list, 2d-arrays, Dictionaries, and APIs
Vocab
Here is some vocab during the lesson, you should be familar with them already no need for me to read these out, now I will pass the speaking off to Kush
- Iteration: A process that repates itself
- Array: Sometimes called a list, can keep strings and intergers inside it
- 2D-Array: A collection of data elements arranged in a grid-like structure with rows and columns
- Mutable: the ability to be changed or modified
- Key: A Singular identifier that is associated with a certin value
Iteration Game
- Link to the game
- Play the levels (only play the first 2 in class)
- Explain how the game relates to itertation
- because you are using loops
import random
word_list = ["python", "computer", "programming", "algorithm", "database", "function", "variable", "loop", "iteration", "array", "mutable", "insertion", "deletion", "key", "API"]
word = random.choice(word_list)
scrambled_word = "".join(random.sample(word, len(word)))
print(f"Unscramble the following Computer Science Word: {scrambled_word}")
hints = 1
guesses = 1
guess = ""
while guess != word and guesses <= 4:
guess = input("What's the unscrambled word? ").lower()
if guess != word:
print("Sorry, that's not the word. Try again!")
if guesses == 1:
guesses += 1
elif guesses == 2:
print(f"Hint 1: The first letter of the word is '{word[0]}'")
guesses += 1
elif guesses == 3:
print(f"Hint 2: The second letter of the word is '{word[1]}'")
guesses += 1
else:
print(f"All 4 Guesses have been used, you didn't unscramble the word, the word was {word}")
guesses += 1
else:
print("Congratulations, you unscrambled the word!")
rows = 5
for i in range(rows):
# print spaces before asterisks to create pyramid shape
for j in range(rows-i-1):
print(" ", end="")
# print asterisks in increasing order
for j in range(i+1):
print("* ", end="")
# move to next line after printing each row
print()
2D array 0.2 (can get up to 0.23)
- Explain how the tic tac toe game works Tic Tac Toe game works by defining the variable "board" with 3 different lists (which is a 2D array.) It then defines a funtion where it prints the board and a function that checks if there is a win or a tie. Finally, it defines the function "play_game" that has a while loop that contains conditionals. It takes the user's inut and calls the functions defined earlier.
- Give 3 Examples of games that can be made from 2D arrays
Minesweeper: Minesweeper is a popular single-player game that involves uncovering cells on a grid to find hidden mines. The game is played on a 2D array, where each cell can either be empty or contain a mine. The player's objective is to reveal all the cells that do not contain mines without detonating any mines. The player can deduce the location of mines by using the numbers on the revealed cells, which indicate the number of adjacent cells that contain mines.
Battleship: Battleship is a two-player game that involves guessing the location of the opponent's hidden ships on a 2D grid. Each player has a grid where they place their ships, and the opposing player tries to guess the location of the ships by selecting cells on their own grid. The game is played on a 2D array, where each cell can either be empty or contain a ship. The objective of the game is to sink all the opponent's ships.
Snake: Snake is a classic arcade game where the player controls a snake that moves around a 2D grid, eating food and growing in length. The game is played on a 2D array, where each cell can either be empty or contain food. The snake moves continuously in a particular direction, and the player must steer the snake to avoid running into walls or its own tail. The objective of the game is to eat as much food as possible without crashing the snake.
List and Dictionaries 0.2 (can get up to 0.23)
The difference between lists and dictionaries: lists use [] dictionaries use {} lists have items dictionaries have keys and values lists are ordered dictionaries are unordered Lists can contain elements of any data type (dictionaries can't with keys) Lists can be accessed with index numbers Dictionaries can be accessed with keys
Make a code block that manipulates either a list or a dictionary
numbers = [5, 1, 4, 2, 8]
sorted_numbers = sorted(numbers)
print(sorted_numbers)