1/25
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai |
|---|
No study sessions yet.
Basic Commands
Built-in Karel commands, case sensitivity matters, move(), turn_left(), put_ball()
Functions
Defining new commands, are created using the ‘def’ keyword. The structure is ‘def function_name():’ followed by an indented block of code. Helps break down programs into smaller groups (top-down design), avoid repeating code, and make the program more readable.
Function Definition and Calling
A function must be defined once before it can be called multiple times in the program.
Syntax Error
Defining the same function twice
Top-Down Design
This is a method of designing a program by starting with the biggest problem and breaking it down into smaller, easier-to-solve sub-problems (functions)
Comments
A single-line that is created using the pound sign (#)
Indentation
Is mandatory and is used to define a code block (or “body”) for functions, ‘for’ loops, ‘while’ loops, and ‘if’ statements. It clearly shows the structure of the code and what commands belong together
‘for’ Loop
Used to repeat a fixed set of commands a fixed number of times. Ideal for tasks where you know the exact number of repetitions. Ex: for I in range():
‘while’ Loop
Used to repeat commands as long as a certain condition is true. The loop continues to run until the condition becomes false. Ex: while facing_north():
if/else
Used to run a command or block of code only if a condition is true. The else block runs only if the condition is false. Ex: if facing_north(): move() else: face_north()
Conditions
These are commands that return a “true” or “false” value and are used in ‘while’ and ‘if’ statements. Ex: balls_present(), no_balls_present()
Counting repeats
When a command is inside a ‘for I in range (n):’ loop, it executes n times. If the command is outside the loop, it executes once
The Last Spot Problem (cleanup Karel)
when a ‘while front_is_clear():’ loop is used to reaverse a row, the loop stops before Karel moves onto the final location. Any action that needs to be performed on the final spot must be handled by any ‘if’ statement placed after the ‘while’ loop.
Variable Assignment
The correct way to store a value is using the assignment operator (variable_name = value). The variable name must start with a letter or underscore and is typically written in all lowercase with underscores.
Snake Case
All lowercase with underscores
‘getting’ input
always reads user input as a string
string
text
Data type conversion
To use user input as a number in math calculations, you must convert the string to an integer using the int() function: number_var = int(input(“prompt: “)).
Output and Concatenation
the print() function displays output. Using the + operator on 2 strings results in string concatenation.
String Concatenation
Joining 2 strings together without a space EX: “Hello” + “World” = “HelloWorld”
Canvas Coordinates
The standard graphics canvas uses the Cartesian coordinate system.
Circle Placement
When using the ‘circle’ object ‘set_position(x, y)’ , the ‘(x, y)’ coordinates define the center of the cirlce
Modulus %
Calculates the remainder of a division Ex: 18 ‘MOD’ 4 is 2
Input()
Used to read a value from the user
Displace()
Used to show output