Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## Imports
- from guizero import App, TextBox, PushButton, Picture, Text, ButtonGroup
- from random import randrange
- ## Create a list of questions
- questions = ['What is 1+1?', 'What is 2+2?', "What is 3+3?", "What is 4+4?"]
- answers = ["2", '4', '6', '8']
- images = ["0.jpg", "1.jpg", "2.jpg", "3.jpg" ]
- ## Function to call at start of quiz
- def start():
- question.index_value = 0
- question.value = questions[question.index_value]
- picture.value = images[question.index_value]
- check_answer.show()
- #hiding butons that don't need to be visible yet
- start.hide()
- choice.show()
- picture.show()
- ## Check if the answer in the box or the multiple choice is identical to the answer in the list
- def check():
- if input_box.value == answers[question.index_value]:
- question.value = 'Correct'
- check_answer.bg="#5A7B5A"
- next.show()
- elif choice.value == answers[question.index_value]:
- question.value = 'Click Next'
- check_answer.bg="#5A7B5A"
- next.show()
- check_answer.hide()
- else:
- question.value = "Incorrect. Answer and check again."
- check_answer.bg="red"
- next.hide()
- ## Function to progress through the list values
- def next():
- if question.index_value == len(questions)-1:
- question.value = "Thanks for playing!"
- next.hide()
- check_answer.hide()
- choice.hide()
- input_box.hide()
- picture.value = "congats.png"
- start_again.show()
- #return
- else:
- question.index_value += 1
- question.value = questions[question.index_value]
- picture.value = images[question.index_value]
- check_answer.show()
- ## Create and App
- app = App(title='Quiz - Answer in text or use the options.', width = 550, layout="grid", bg = "#9932CC")
- ## Text for the question
- question = Text(app, text='Click on image to start the quiz.', grid=[0,1])
- ## Box for the answer
- input_box = TextBox(app, text='Answer', grid=[0,2])
- ## Button to check the answer
- check_answer = PushButton(app, command = check, text='Check answer', grid=[0,3])
- ## Hide the button until we start
- check_answer.hide()
- ## Button to start the quiz, then used as Next from that point onwards
- start = PushButton(app, command=start, text='Start', grid=[0,4], image="start-test.jpg")
- ## Dispaly a start picture under the Start button
- picture = Picture(app, image="start-test.jpg", grid=[1,1])
- picture.hide()
- ## Next button to use the next function and increment the index
- next = PushButton(app, command=next, text='Next', grid=[0,6])
- next.hide()
- ## Multiple choice radio buttons, answer and 1 extra wrong answer (16)
- choice = ButtonGroup(app, options=["2", "4", "6", "8", "16"], horizontal=True, selected=5, command=check, grid=[0,7])
- choice.hide()
- start_again = PushButton(app, command=start, text='Start again?', grid=[0,6])
- start_again.hide()
- app.display()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement