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
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
mzxs1g21
COMP2215 - Guess number game
Commits
11f7355f
Commit
11f7355f
authored
2 years ago
by
mzxs1g21
Browse files
Options
Downloads
Patches
Plain Diff
Replace Guess_number_skeleton.py
parent
a8775b35
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
Guess_number_skeleton.py
+45
-8
45 additions, 8 deletions
Guess_number_skeleton.py
with
45 additions
and
8 deletions
Guess_number_skeleton.py
+
45
−
8
View file @
11f7355f
...
@@ -15,16 +15,32 @@ def get_guess():
...
@@ -15,16 +15,32 @@ def get_guess():
guess
=
1
guess
=
1
while
True
:
while
True
:
if
button1
.
value
()
==
0
:
if
button1
.
value
()
==
0
:
# Logic to increment guess by 1 and print it (also implement error handling for guess being > 10)
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
:
if
button2
.
value
()
==
0
:
guess
-=
1
guess
-=
1
# Logic to decrement guess by 1 and print it (also implement error handling for guess being < 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
:
if
button3
.
value
()
==
0
:
print
(
"
Submitting guess...
"
)
print
(
"
Submitting guess...
"
)
return
guess
return
guess
def
get_result
(
guess
,
answer
):
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)
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
():
def
play_game
():
answer
=
random
.
randint
(
1
,
10
)
answer
=
random
.
randint
(
1
,
10
)
...
@@ -33,12 +49,33 @@ def play_game():
...
@@ -33,12 +49,33 @@ def play_game():
guess
=
get_guess
()
guess
=
get_guess
()
result
=
get_result
(
guess
,
answer
)
result
=
get_result
(
guess
,
answer
)
if
result
==
"
green
"
:
if
result
==
"
green
"
:
# Actions taken if guess is the same as answer
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
"
:
elif
result
==
"
yellow
"
:
# Actions taken if guess is quite close to answer
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
"
:
elif
result
==
"
orange
"
:
# Actions taken if guess is somewhat close to answer
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
"
:
elif
result
==
"
red
"
:
# Actions taken if guess is far from answer
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
()
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