Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Add library packages
- from guizero import App, TextBox, PushButton, Picture, Text
- from random import randrange
- # define variable questions - a list of questions
- questions = ['What Python function is equivalent to the Scratch "say" block ?',
- 'What Python module contains the randint function ?',
- 'Fill in the blank - pseudo____ ?']
- # define the list of matching answers
- answers = ['print',
- 'random',
- 'code']
- # function to start the quiz
- def start():
- # pick a random question in the range determined by the number of questions in the list.
- input_box.value = ""
- question.index_value = randrange(len(questions))
- question.value = questions[question.index_value]
- # change text on button to next after start
- start.text = 'Next'
- # and show the check button having started
- check_answer.show()
- # function to check answer in input box against answer in list
- # and set value to display
- def check():
- if input_box.value == answers[question.index_value]:
- question.value = 'Correct'
- else:
- question.value = 'Incorrect'
- input_box.value = ""
- # setup gui window
- app = App(title='Quiz', width=1000, height=500)
- # add a line of text to the gui
- question = Text(app, text='Ready to start the quiz?')
- # and a text box for the answers
- input_box = TextBox(app, text='Type answers here', width=20)
- # call the check function when the push button widget is pushed
- check_answer = PushButton(app, command = check, text='Check answer')
- # hide the check pushbutton until a question is asked
- check_answer.hide()
- # pick the next question using the start function when button is pushed
- start = PushButton(app, command=start, text='Start')
- app.display()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement