Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
C
COMP2215 - Guess number game
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
mzxs1g21
COMP2215 - Guess number game
Commits
49856052
Commit
49856052
authored
2 years ago
by
mzxs1g21
Browse files
Options
Downloads
Patches
Plain Diff
Upload New File
parents
No related branches found
No related tags found
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
Guess_number_skeleton.py
+44
-0
44 additions, 0 deletions
Guess_number_skeleton.py
with
44 additions
and
0 deletions
Guess_number_skeleton.py
0 → 100644
+
44
−
0
View file @
49856052
from
machine
import
Pin
import
random
import
time
import
neopixel
np
=
neopixel
.
NeoPixel
(
machine
.
Pin
(
28
),
1
)
np
[
0
]
=
(
0
,
0
,
0
)
# Initially set the led to nothing
np
.
write
()
button1
=
Pin
(
20
,
Pin
.
IN
,
Pin
.
PULL_DOWN
)
button2
=
Pin
(
21
,
Pin
.
IN
,
Pin
.
PULL_DOWN
)
button3
=
Pin
(
22
,
Pin
.
IN
,
Pin
.
PULL_DOWN
)
def
get_guess
():
guess
=
1
while
True
:
if
button1
.
value
()
==
0
:
# Logic to increment guess by 1 and print it (also implement error handling for guess being > 10)
if
button2
.
value
()
==
0
:
guess
-=
1
# Logic to decrement guess by 1 and print it (also implement error handling for guess being < 1)
if
button3
.
value
()
==
0
:
print
(
"
Submitting guess...
"
)
return
guess
def
get_result
(
guess
,
answer
):
# If statements to assign colour of LED depending on how far guess is from answer (distacnce up to individual, 4 colours)
def
play_game
():
answer
=
random
.
randint
(
1
,
10
)
print
(
"
Guess a number between 1 and 10!
"
)
while
True
:
guess
=
get_guess
()
result
=
get_result
(
guess
,
answer
)
if
result
==
"
green
"
:
# Actions taken if guess is the same as answer
elif
result
==
"
yellow
"
:
# Actions taken if guess is quite close to answer
elif
result
==
"
orange
"
:
# Actions taken if guess is somewhat close to answer
elif
result
==
"
red
"
:
# Actions taken if guess is far from answer
play_game
()
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment