Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 4 additions & 10 deletions lessons/01-flowcharts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,15 @@ section: "Programming with Blocks"

# One step at a time

Before we write a single line of Python, let's get the big idea out of the way:
**a program is a list of instructions, and the computer runs them one at a time,
in order.** It never skips ahead, and it never runs two steps at once by
accident. It finishes one box, updates its memory, and follows the arrow to the
next box.
Your robot is powering on for the first time, and it knows nothing about FRC yet. Before we write a single line of Python, let's get the big idea out of the way: **a program is a list of instructions, and the robot runs them one at a time, in order.** It never skips ahead, and it never runs two steps at once by accident. It finishes one box, updates its memory, and follows the arrow to the next box.

A **flowchart** shows that exact journey from **START** to **END**:

- Each **box** is one instruction — like storing a number or doing math.
- Each **box** is one instruction, like storing a number or doing math.
- Each **arrow** shows which step runs next.
- **Robot Memory** at the bottom shows the named values the robot is remembering
right now.
- **Robot Memory** at the bottom shows the named values the robot is remembering right now.

The flowchart here calculates an FRC robot's **autonomous score**: `3` coral at
`4` points each, plus a `3`-point bonus for leaving the starting zone.
The flowchart here teaches the robot how an FRC **autonomous score** works: `3` coral at `4` points each, plus a `3`-point bonus for leaving the starting zone.

Press **▶ Run** (or click **⏭ Step** to advance one instruction at a time) and
watch **Robot Memory** update as each box lights up. You can also click
Expand Down
32 changes: 16 additions & 16 deletions lessons/05-what-is-java/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ section: "Python Fundamentals"

# A language, not a magic spell

You've been building programs already, as flowcharts of blocks ([lesson {n}](#/lesson/01-flowcharts) onward). From here on we write those same ideas as **text**, in a language called **Python**.
Your robot has been learning the basics through block flowcharts ([lesson {n}](#/lesson/01-flowcharts) onward). From here on we write those same robot instructions as **text**, in a language called **Python**.

Why does FRC 2713 teach software fundamentals in Python? Python reads close to plain English and keeps extra boilerplate out of your way, so you can focus on the core ideas: variables, decisions, loops, functions, objects, and state machines. Those concepts carry straight over to any language you use on the team later, including Java for robot code, and Python itself is useful for scripts, data analysis, and team tools.

A programming language is a strict set of grammar rules. English is forgiving. "Computer the on it run" is garbled, yet a person can still guess what you meant. Python won't guess. Every instruction has to follow the rules, or Python stops and points to the exact line that confused it. That isn't the computer being difficult; it's the computer telling you what it needs fixed.
A programming language is a strict set of grammar rules. English is forgiving. "Robot the on it run" is garbled, yet a person can still guess what you meant. Python won't guess. Every instruction has to follow the rules, or Python stops and points to the exact line that confused it. That isn't the computer being difficult; it's the computer telling you what it needs fixed.

Two habits matter right from the start:

- **Python is case-sensitive.** `Print` and `print` are different words to Python, just like "Rex" and "rex" are different if one is a name and the other isn't. Spelling has to match, capital letters included.
- **Python is case-sensitive.** `Print` and `print` are different words to Python, just like `"Intake"` and `"intake"` are different strings. Spelling has to match, capital letters included.
- **Indentation matters.** Python uses spaces at the start of a line to group steps that belong together. Lining code up isn't just for neatness here; it's part of the grammar.

# The punctuation you'll see everywhere
Expand All @@ -24,37 +24,37 @@ Before you write a single instruction, it helps to recognize the symbols Python

```text
: colon - starts an indented block of steps
( ) parentheses - hold the "ingredients" a function needs
, comma - separates ingredients when there is more than one
( ) parentheses - hold the inputs a function needs
, comma - separates inputs when there is more than one
" " quotes - mark exact text, called a "string"
# hash - starts a comment (a note for humans)
```

- **Colons `:`** sit at the end of a header line to say "an indented block of steps comes next." You'll see them on `if` statements, loops, and functions.
- **Parentheses `( )`** hold what an instruction needs to do its job. Some instructions need nothing (`()`), some need one thing, and some need several.
- **Commas `,`** separate those things when an instruction needs more than one, the same job a comma does in "eggs, milk, and flour."
- **Commas `,`** separate those things when an instruction needs more than one, the same job a comma does in "intake, shooter, and climber."
- **Quotes `" "`** mark exact text for Python to use as-is, called a **string**. Anything inside the quotes is data, not instructions, so Python won't try to run it.
- **Hash `#`** starts a **comment**. Anything after `#` on a line is a note for humans, and Python skips right over it.

# Reading a program's shape

Here is a tiny Python program so you can see those symbols working together:
Here is a tiny Python program for our robot so you can see those symbols working together:

```text
# a short Python program
def greet(name):
print("Hello, " + name)
# announce the robot's current match mode
def announce(mode):
print("Robot mode: " + mode)

greet("Ada")
announce("auto")
```

Don't worry about memorizing this yet. You'll learn to write your own functions in [lesson {n}](#/lesson/11-writing-methods). For today, just spot the pieces using the vocabulary above:

- `# a short Python program` is a comment that Python ignores.
- `def greet(name):` defines a reusable block named `greet` that expects one input, `name`. The colon at the end tells Python an indented block is coming next.
- ` print("Hello, " + name)` is indented by four spaces, so it belongs *inside* `greet`.
- `greet("Ada")` is back at the left edge, outside `greet`, so Python runs it and hands `"Ada"` into the function.
- `# announce the robot's current match mode` is a comment that Python ignores.
- `def announce(mode):` defines a reusable block named `announce` that expects one input, `mode`. The colon at the end tells Python an indented block is coming next.
- ` print("Robot mode: " + mode)` is indented by four spaces, so it belongs *inside* `announce`.
- `announce("auto")` is back at the left edge, outside `announce`, so Python runs it and hands `"auto"` into the function.

Notice how indentation shows what belongs to what, just like a box drawn around steps in a flowchart. Python doesn't require an outer wrapper class to start running code, so a one-line script is already a valid program.

You now know enough vocabulary to read Python without getting tripped up by the syntax. Next lesson, you'll write and run your first real instruction.
You now know enough vocabulary to read Python without getting tripped up by the syntax. Next lesson, you'll run the robot's first real instruction.
10 changes: 5 additions & 5 deletions lessons/06-hello-world/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@ You've already been building programs as flowcharts of blocks. Everything you di

A **program** is a list of instructions, written in a language a computer can follow step by step, in order.

That's it. Not magic, just a recipe: "Crack two eggs. Whisk them. Pour into a hot pan." A computer program is the same idea, except the kitchen is the computer's memory and screen, and the chef is the computer itself, which follows your instructions *exactly* as written, including your mistakes.
That's it. Not magic, just a pre-match checklist: "Zero the gyro. Spin up the flywheel. Drive forward two meters." A robot program is the same idea, except the robot controller follows your instructions *exactly* as written, including your mistakes.

Everything you learn here builds the foundation you'll use for FRC robot software, simulations, and team tools.

# Your first program

Here is a complete, one-line Python program:
When a robot boots up on the workbench, the first thing you want to know is that it is alive and talking to the driver station. Here is a complete, one-line Python program that prints its first message:

```python
print("Hello, world!")
```

`print` is a function that means "show this on the screen." The text in quotes is exactly what gets shown. Computers don't know that `Hello, world!` is a greeting; they just do what they're told with the data they're given. Each instruction sits on its own line, with no semicolon needed at the end.
`print` is a function that means "show this on the console." The text in quotes is exactly what gets shown. The robot doesn't know that `Hello, world!` is a greeting; it just does what it's told with the data it's given. Each instruction sits on its own line, with no semicolon needed at the end.

Two things to know about the playground before you press Run:

Expand All @@ -36,10 +36,10 @@ Now break it on purpose. Try changing the text between the quotes, removing a qu

# Your turn

Edit the code below so it prints a greeting with **your own name** in it, instead of `world`. Run it to check.
Edit the code below so the robot announces itself to the driver station with **your robot's name or team number** instead of `world`. Run it to check.

```python
print("Hello, world!")
```

Once that works, add a second line that prints something else about yourself, like your favorite subject or a hobby. Each `print(...)` line runs in order, top to bottom, and produces its own line of output.
Once that works, add a second line that prints a startup status, like `"Battery checked"` or `"Autonomous armed"`. Each `print(...)` line runs in order, top to bottom, and produces its own line of output.
22 changes: 11 additions & 11 deletions lessons/07-variables/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@ section: "Python Fundamentals"

# A name for a value

Back in [the very first lesson](#/lesson/01-flowcharts), every **number block** held a value, and arrows carried that value forward to the blocks that needed it. In Python we do the same thing with a **variable**: a name that stands for a value.
Back in [the very first lesson](#/lesson/01-flowcharts), every box stored values in **Robot Memory**, and later steps read them back. In Python we do the same thing with a **variable**: a name that stands for a value.

```python
name = "Ada"
print("Hello, " + name)
robot_name = "Red Hawk"
print("Booting " + robot_name)
```

Read the `=` as "gets" or "is set to," **not** as "equals" from math. It means: take the value on the right, and store it under the name on the left. After that line runs, the name `name` holds the text `"Ada"`, and anywhere you write `name` Python reaches in and uses that value.
Read the `=` as "gets" or "is set to," **not** as "equals" from math. It means: take the value on the right, and store it under the name on the left. After that line runs, `robot_name` holds the text `"Red Hawk"`, and anywhere you write `robot_name` Python reaches in and uses that value.

Every value in Python has a **type**, which is the *kind* of value it is. `"Ada"` is a `str` (short for *string*, meaning text). Python figures out the type from the value you assign, so you don't have to write `str` in front of `name`, and it still tracks the type behind the scenes. More on types in [lesson {n}](#/lesson/08-numbers-and-text).
Every value in Python has a **type**, which is the *kind* of value it is. `"Red Hawk"` is a `str` (short for *string*, meaning text). Python figures out the type from the value you assign, so you don't have to write `str` in front of `robot_name`, and it still tracks the type behind the scenes. More on types in [lesson {n}](#/lesson/08-numbers-and-text).

The `+` between `"Hello, "` and `name` **joins** text together into one string, so this program prints `Hello, Ada`.
The `+` between `"Booting "` and `robot_name` **joins** text together into one string, so this program prints `Booting Red Hawk`.

Press **Run**. Then change `"Ada"` to your own name and run again. Notice you only had to change it in **one place**, even though the name gets *used* on the next line. That's the whole point of giving a value a name.
Press **Run**. Then change `"Red Hawk"` to a name for your own robot and run again. Notice you only had to change it in **one place**, even though the name gets *used* on the next line. That's the whole point of giving a value a name.

# Use a name as many times as you like

Expand All @@ -41,19 +41,19 @@ You can name a variable almost anything, as long as it starts with a letter or u

# A variable can vary

Here's where the name earns the word *variable*: the value it holds can **change** while the program runs. Assigning to a name a second time replaces what was there before.
Here's where the name earns the word *variable*: the value it holds can **change** while a match runs. Assigning to a name a second time replaces what was there before.

```python
score = 0
print(f"Starting score: {score}")
print(f"Match start: {score}")

score = 10
print(f"After scoring: {score}")
print(f"After autonomous: {score}")

score = score + 5
print(f"After a bonus: {score}")
```

Read those last two assignments carefully. `score = 10` throws away the old `0` and stores `10`. Then `score = score + 5` looks strange until you remember `=` means "gets set to," not "equals." Python works out the right side first (`10 + 5 = 15`), then stores that back into `score`. This "take the value, change it, put it back" pattern is the same running total the **repeat** block kept in [lesson {n}](#/lesson/03-loops), a value that updates itself one step at a time.

Your turn: add a line that gives a `10` point penalty (subtract, don't add), then print the score again. Predict the final number before you run it.
Your turn: add a line that takes a `10`-point foul penalty (subtract, don't add), then print the score again. Predict the final number before you run it.
14 changes: 7 additions & 7 deletions lessons/08-numbers-and-text/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,25 @@ Run it. The lesson: the *quotes* decide everything. `7` is a number you can add;

# Text that looks like a number

Sometimes a value *looks* like a number but is really text because it's wrapped in quotes, like `"24"`. If you try `"24" + 1` in Python, it stops with a `TypeError` because Python refuses to guess whether you meant `"241"` or `25`.
Sometimes the field management system or driver station sends a value that *looks* like a number but arrives as text wrapped in quotes, like `"24"`. If you try `"24" + 1` in Python, it stops with a `TypeError` because Python refuses to guess whether you meant `"241"` or `25`.

The fix is to **convert** the text into a real number with `int(...)`:

```python
age_text = "24"
age = int(age_text)
print(f"Next year you will be {age + 1}")
match_text = "24"
match_number = int(match_text)
print(f"Next up: qualification match {match_number + 1}")
```

Using a separate name for the converted number (`age_text` for the `str`, `age` for the `int`) keeps the program easy to read. You can tell at a glance what is text and what is a number, building on the naming habit from [lesson {n}](#/lesson/07-variables). Inside the f-string, Python evaluates `age + 1` first before inserting the result, just like inner-first evaluation in [lesson {n}](#/lesson/01-flowcharts).
Using a separate name for the converted number (`match_text` for the `str`, `match_number` for the `int`) keeps the program easy to read. You can tell at a glance what is text and what is a number, building on the naming habit from [lesson {n}](#/lesson/07-variables). Inside the f-string, Python evaluates `match_number + 1` first before inserting the result, just like inner-first evaluation in [lesson {n}](#/lesson/01-flowcharts).

Conversion goes the other way too: `str(42)` turns a number into the text `"42"`, which is handy whenever you want to join a number onto a string with `+` instead of an f-string.

One more type while we're here: numbers with a decimal point, like a motor voltage of `12.5`, use the type **`float`** (short for *floating-point number*):
One more type while we're here: numbers with a decimal point, like a robot battery voltage of `12.5`, use the type **`float`** (short for *floating-point number*):

```python
voltage = 12.5
print(f"Battery: {voltage} volts")
```

Your turn: start with the text `"9"`, convert it with `int(...)`, and print that number **plus 5**. Then try removing `int(...)` for a moment and running it to see the `TypeError` Python gives when you mix text and numbers.
Your turn: start with a dashboard reading `coral_text = "9"`, convert it with `int(...)`, and print that number **plus 5** more scored in teleop. Then try removing `int(...)` for a moment and running it to see the `TypeError` Python gives when you mix text and numbers.
Loading
Loading