From 11f7355ff6c28c28c635e7f58fd1aff6882a166f Mon Sep 17 00:00:00 2001 From: mzxs1g21 <mzxs1g21@soton.ac.uk> Date: Sun, 14 May 2023 00:58:28 +0000 Subject: [PATCH] Replace Guess_number_skeleton.py --- Guess_number_skeleton.py | 53 ++++++++++++++++++++++++++++++++++------ 1 file changed, 45 insertions(+), 8 deletions(-) diff --git a/Guess_number_skeleton.py b/Guess_number_skeleton.py index 020ce3c..cb28757 100644 --- a/Guess_number_skeleton.py +++ b/Guess_number_skeleton.py @@ -15,16 +15,32 @@ 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) + 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 - # 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: 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) + 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) @@ -33,12 +49,33 @@ def play_game(): guess = get_guess() result = get_result(guess, answer) 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": - # 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": - # 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": - # 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 -- GitLab