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
a8775b35
Commit
a8775b35
authored
2 years ago
by
mzxs1g21
Browse files
Options
Downloads
Patches
Plain Diff
Upload New File
parent
49856052
No related branches found
No related tags found
Loading
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
Guess_number.py
+81
-0
81 additions, 0 deletions
Guess_number.py
with
81 additions
and
0 deletions
Guess_number.py
0 → 100644
+
81
−
0
View file @
a8775b35
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
:
guess
+=
1
if
guess
>
10
:
print
(
"
Your guess can
'
t be greater than 10.
"
)
guess
-=
1
print
(
"
Guess:
"
,
guess
)
time
.
sleep
(
0.2
)
if
button2
.
value
()
==
0
:
guess
-=
1
if
guess
==
0
:
print
(
"
Your guess can
'
t be less than or equal to 0.
"
)
guess
+=
1
print
(
"
Guess:
"
,
guess
)
time
.
sleep
(
0.2
)
if
button3
.
value
()
==
0
:
print
(
"
Submitting guess...
"
)
return
guess
def
get_result
(
guess
,
answer
):
if
guess
==
answer
:
return
"
green
"
elif
abs
(
guess
-
answer
)
<=
2
:
return
"
yellow
"
elif
abs
(
guess
-
answer
)
<=
4
:
return
"
orange
"
elif
abs
(
guess
-
answer
)
>
4
:
return
"
red
"
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
"
:
print
(
"
You win! The number was indeed
"
,
answer
)
np
[
0
]
=
(
0
,
255
,
0
)
# Change LED colour to green
np
.
write
()
time
.
sleep
(
1
)
np
[
0
]
=
(
0
,
0
,
0
)
np
.
write
()
break
elif
result
==
"
yellow
"
:
print
(
"
You
'
re getting pretty hot!
"
)
np
[
0
]
=
(
255
,
255
,
0
)
# Change LED colour to yellow
np
.
write
()
time
.
sleep
(
1
)
np
[
0
]
=
(
0
,
0
,
0
)
np
.
write
()
elif
result
==
"
orange
"
:
print
(
"
You
'
re getting warm! Try again.
"
)
np
[
0
]
=
(
255
,
165
,
0
)
# Change LED colour to orange
np
.
write
()
time
.
sleep
(
1
)
np
[
0
]
=
(
0
,
0
,
0
)
np
.
write
()
elif
result
==
"
red
"
:
print
(
"
You
'
re so cold! Try again!
"
)
np
[
0
]
=
(
255
,
0
,
0
)
# Change LED colour to red
np
.
write
()
time
.
sleep
(
1
)
np
[
0
]
=
(
0
,
0
,
0
)
np
.
write
()
play_game
()
\ No newline at end of file
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