from machine import Pin
import utime, time, random
print('start')
red=Pin(28, Pin.OUT)
red.off()
green=Pin(27, Pin.OUT)
green.off()
keys = [[1, 2, 3, 'A'],
[4, 5, 6, 'B'],
[7, 8, 9, 'C'],
['*', 0, '#', 'D']]
rows = [26, 22,21, 20]
columns = [19,18,17,16]
col_pins = []
row_pins = []
for x in range(0,4):
row_pins.append(Pin(rows[x], Pin.OUT))
col_pins.append(Pin(columns[x], Pin.IN, Pin.PULL_DOWN))
print("Please enter correct answer endig with * mark")
score = 0
main = True
while main:
a = random.randint(1,101)
b = random.randint(1,101)
print( f'{a} + {b} = ?')
answer = ''
status = True
while status:
for row in range(4):
row_pins[row].value(1)
for col in range(4):
if col_pins[col].value():
if keys[row][col] == '*':
status = False
break
answer = answer+ str(keys[row][col])
print("You have pressed:", keys[row][col])
while col_pins[col].value(): # 계속 눌려져 있을 때 오류 방지
pass
#break
utime.sleep(0.3)
# if status==False:
# break
row_pins[row].value(0)
if answer == str(a+b):
print('Correct Answer')
score = score + 1
for i in range(3):
green.on()
time.sleep(0.5)
green.off()
time.sleep(0.5)
else:
print('BaaaaBoooooo')
for i in range(3):
red.on()
time.sleep(0.5)
red.off()
time.sleep(0.5)
main = False
time.sleep(0.2)
print(f'Your score is {score}!')