Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # CodeSkulptor runs Python programs in your browser.
- # Click the upper left button to run this simple demo.
- # CodeSkulptor is tested to run in recent versions of
- # Chrome, Firefox, and Safari.
- #https://py3.codeskulptor.org/#user302_u8p2IrZei3OtDaK.py
- import simplegui
- import random
- import time
- started = False
- message = "Good Luck!"
- x = 0
- y = 0
- ans = 0
- user_ans = -1
- attempts = 0
- max_attempts = 20
- correct = 0
- times = []
- ans_time = -float('inf')
- ans_color = 'Green'
- ans_message = ''
- font_size = 48
- color = 'Red'
- start_point_1 = (120, 20)
- end_point_1 = (120,20)
- end_point_2 = (480, 20)
- nums = [3,4,6,7,8,9,12]
- xnums = list(range(13))
- xnums.extend(nums)
- random.shuffle(xnums)
- xnums_gen = (x_num for x_num in xnums)
- miss_list = []
- def gen_prob():
- global x,y,ans
- x = next(xnums_gen)
- y = random.choice(nums)
- ans = x*y
- display_problem(x,y)
- def display_problem(x,y):
- global message
- #inp.set_text("")
- message = "{} x {} = ?".format(str(x), str(y))
- def display_ans():
- global color, message
- color = temp_color
- def miss():
- global miss_list, ans_time, ans_color, ans_message
- miss_list.append((x,y,x*y))
- ans_time = time.time()
- ans_color = 'Red'
- ans_message = "{} x {} = {}".format(x,y,ans)
- def print_misses(miss_list):
- print("Study These:")
- for miss in miss_list:
- x = str(miss[0])
- y = str(miss[1])
- ans = str(miss[2])
- print("{} x {} = {}".format(x,y,ans))
- def hit():
- global ans_time, ans_message, ans_color
- ans_time = time.time()
- ans_color = 'Green'
- ans_message = "{} x {} = {}".format(x,y,ans)
- def grade():
- global message, started, times, font_size
- #inp.set_text("")
- started = False
- runtime = int(time.time() - t1)
- grade = 100*float(correct)/float(attempts)
- message = "Grade: {} Time: {} ".format(grade, runtime)
- if miss_list:
- print_misses(miss_list)
- if grade > 90:
- times.append(runtime)
- if len(times) ==3:
- font_size = 30
- message = "Your three times were: {}, {}, {}".format(*times)
- print(message)
- def check_answer():
- if not started:
- return
- global ans, user_ans, attempts, correct, end_point_1, color, ans_message, ans_color, ans_time
- attempts += 1
- end_point_1 = (start_point_1[0] + (end_point_2[0]-start_point_1[0]) * attempts/max_attempts, end_point_1[1])
- if user_ans == ans:
- correct += 1
- hit()
- else:
- miss()
- user_ans = None
- if attempts < max_attempts:
- gen_prob()
- else:
- grade()
- return
- # Handler for mouse click
- def click():
- global t1, attempts, correct, started
- started = True
- attempts = 0
- correct = 0
- t1 = time.time()
- gen_prob()
- def key_handler(key):
- global user_ans
- if key == 13:
- check_answer()
- elif key < 58 and key > 47:
- char = chr(key)
- if user_ans <0:
- user_ans = int(char)
- else:
- user_ans = int(str(user_ans) + char)
- elif key == 8:
- user_ans = -1
- else:
- pass
- # Handler to draw on canvas
- def draw(canvas):
- check_time = ans_time + 1
- if ans_color == 'Red':
- check_time += 2
- if time.time() > check_time:
- canvas.draw_text(message, [112,112], font_size, color)
- else:
- canvas.draw_text(ans_message, [112,112], font_size, ans_color)
- if ans_color == 'Red':
- canvas.draw_text('INCORRECT :(', [112,212], font_size-5, ans_color)
- else:
- canvas.draw_text('CORRECT :)', [112,212], font_size-5, ans_color)
- if started:
- if user_ans >= 0:
- canvas.draw_text(str(user_ans), [112,212], 48, "Red")
- canvas.draw_line(start_point_1, end_point_1, 10, "Grey")
- canvas.draw_line(end_point_1, end_point_2, 10, "White")
- # Create a frame and assign callbacks to event handlers
- frame = simplegui.create_frame("Home", 600, 300)
- frame.add_button("Start", click)
- frame.set_draw_handler(draw)
- #inp = frame.add_input("Answer", answer_box, 50)
- #inp.set_text("")
- frame.set_keyup_handler(key_handler)
- # Start the frame animation
- frame.start()
Advertisement
Add Comment
Please, Sign In to add comment