Skip to content
Snippets Groups Projects
Commit 11f7355f authored by mzxs1g21's avatar mzxs1g21
Browse files

Replace Guess_number_skeleton.py

parent a8775b35
No related branches found
No related tags found
No related merge requests found
...@@ -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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment