diff --git a/Guess_number.py b/Guess_number.py new file mode 100644 index 0000000000000000000000000000000000000000..cb28757efdf612a9afa1a8e0fed17b4a6039231d --- /dev/null +++ b/Guess_number.py @@ -0,0 +1,81 @@ +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