Skip to content

Instantly share code, notes, and snippets.

@MikeTheWatchGuy
Last active March 25, 2019 15:14
Show Gist options
  • Save MikeTheWatchGuy/ba688341204361c3e694cd9687c4b0e3 to your computer and use it in GitHub Desktop.
Save MikeTheWatchGuy/ba688341204361c3e694cd9687c4b0e3 to your computer and use it in GitHub Desktop.
Demonstrates using PySimpleGUI to create a "flash card" type of mult game
import PySimpleGUI as sg
from random import randint
NUM_GUESSES_ALLOWED = 3
layout = [ [sg.Text('Multiplication Game', text_color='blue')],
[sg.Text('', size=(20,1), font='Arial 20', key='_QUESTION_', text_color='red')],
[sg.Input(size=(4,1), enable_events=True, key='_ANSWER_'), sg.Text('', size=(24,1), text_color='red', key='_OUTPUT_')],
# [sg.Button('Answer', bind_return_key=True), sg.Button('Quit')],
[sg.Text('Change Difficulty Level')],
[ sg.Radio('Easy', 1, key='_EASY_', default=True), sg.Radio('Medium', 1, key='_MED_'), sg.Radio('Hard', 1, key='_HARD_')] ]
window = sg.Window('Multiplication Game',
font='Arial 14').Layout(layout).Finalize()
while True: # Event Loop
var1, var2 = randint(0,10), randint(0,10)
answer = var1 * var2
window.Element('_QUESTION_').Update('What is {} X {}'.format(var1, var2))
guess_count = 0
while True:
event, values = window.Read()
if event in (None, 'Quit'):
window.Close()
exit()
if values['_ANSWER_'] == str(answer):
window.Element('_OUTPUT_').Update('Correct!')
window.Element('_ANSWER_').Update('')
break
if len(values['_ANSWER_']) >= len(str(answer)):
window.Element('_ANSWER_').Update('')
window.Element('_OUTPUT_').Update('{} is not correct'.format(values['_ANSWER_']))
guess_count += 1
if guess_count >= NUM_GUESSES_ALLOWED:
window.Element('_OUTPUT_').Update('Sorry, the answer was {}'.format(answer))
break
@MikeTheWatchGuy
Copy link
Author

image

@MikeTheWatchGuy
Copy link
Author

If removing the difficulty level stuff, you get this window:
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment