Skip to content

Commit

Permalink
Update examples and docs to reflect removal of namespaces
Browse files Browse the repository at this point in the history
Fix all of the code to remove math., time. and random.

Signed-off-by: Keith Packard <keithp@keithp.com>
  • Loading branch information
keith-packard committed Nov 21, 2024
1 parent 38c42d8 commit b8ff418
Show file tree
Hide file tree
Showing 18 changed files with 234 additions and 182 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ described below.

Here's some places that have seen recent work

* Changed the builtin math, time and random functions to no longer
include the math., time. or random. prefixes. This makes them more
convenient to use. To retain compatibility with Python, you can
use 'from math import *' which Snek will ignore.

* [Mu](https://codewith.mu/) version 1.2.0 has Snek support built in
now. Mu is an IDE designed for new Python users that offers a more
polished alternative to the simple Snek IDE provide with snek.
Expand Down
4 changes: 2 additions & 2 deletions doc/lessons/lesson-1/lesson-1-snekboard.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ things in front of it:
----
> *while True:*
+ *print(read(A1))*
+ *time.sleep(1)*
+ *sleep(1)*
+
0.3484738
0.3829738
Expand All @@ -289,7 +289,7 @@ things in front of it:
*Ctrl+C*
----

You'll need to hit kbd:[Enter] after *time.sleep(1)* to get the
You'll need to hit kbd:[Enter] after *sleep(1)* to get the
program to run. Hit kbd:[Ctrl+C] when you're done testing to stop the
program.

Expand Down
2 changes: 1 addition & 1 deletion doc/lessons/lesson-2/lesson-2-line-bug.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ light sensor using something like this (use <Ctrl>C to stop):

while True:
print(read(A1))
time.sleep(1)
sleep(1)

Consider:

Expand Down
8 changes: 4 additions & 4 deletions doc/lessons/lesson-3/bumper-car.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ def WaitUntilCloseOtherDir():


def WaitRandom():
wait = 0.25 + random.randrange(10) / 10
time.sleep(wait)
wait = 0.25 + randrange(10) / 10
sleep(wait)


def Spin():
Expand All @@ -62,15 +62,15 @@ def BumperCar():

# Back up a bit and spin around
MoveOneDir()
time.sleep(0.5)
sleep(0.5)
SpinRandom()

MoveOneDir()
WaitUntilCloseOneDir()

# Back up a bit and spin around
MoveOtherDir()
time.sleep(0.5)
sleep(0.5)
SpinRandom()

MoveOtherDir()
Expand Down
22 changes: 11 additions & 11 deletions doc/lessons/lesson-3/lesson-3-bumper-car.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ distance:
----
> *while True:*
+ *print(read(A8))*
+ *time.sleep(1)*
+ *sleep(1)*
+
0.162149
0.1758242
Expand Down Expand Up @@ -197,30 +197,30 @@ do this, we'll spin in place.
To make our robot less predictable, let's have it spin a random
amount. Random numbers are like the roll of a die; you can't predict
the value. Snek can generate random numbers using the
`random.randrange` function. `random.randrange` takes one value, and
`randrange` function. `randrange` takes one value, and
will generate a random number from zero to one less than this value.
So, if you want a random number from the set { 0, 1, 2, 3, 4, 5 },
you'd use `random.randrange(6)`. We can do arithmetic on the number to
you'd use `randrange(6)`. We can do arithmetic on the number to
adjust the range further.

Let's explore that:

[subs="verbatim,quotes"]
----
> *random.randrange(6)*
> *randrange(6)*
3
> *random.randrange(6)*
> *randrange(6)*
1
> *for i in range(5):*
+ *print(random.randrange(6))*
+ *print(randrange(6))*
+
5
1
4
0
3
> *for i in range(5):*
+ *print(random.randrange(6) + 1)*
+ *print(randrange(6) + 1)*
+
6
5
Expand All @@ -236,8 +236,8 @@ say between 1 and 6 seconds:
[source,python,subs="verbatim,quotes"]
----
def WaitRandom():
wait = random.randrange(6) + 1
time.sleep(wait)
wait = randrange(6) + 1
sleep(wait)
----

Let's figure out how to get the bumper car to spin. To go straight, we
Expand Down Expand Up @@ -277,15 +277,15 @@ def BumperCar():
# Back up a bit and spin around
MoveOneDir()
time.sleep(1)
sleep(1)
SpinRandom()
MoveOneDir()
WaitUntilCloseOneDir()
# Back up a bit and spin around
MoveOtherDir()
time.sleep(1)
sleep(1)
SpinRandom()
MoveOtherDir()
Expand Down
Loading

0 comments on commit b8ff418

Please sign in to comment.