jeudi 15 avril 2021

How can I limit pick a question from a list only once in a random order?

I am trying to modify my flashcard python script. It works fine, but what I am triying to do is to modify the random function so that a single question (and its answer) it is not chosen twice. The script should not continue picking questions after the list is completed. I would also need to add a button that let me brwose back to previuos selections.

Any suggestions?

from tkinter import *
from tkinter import ttk
from random import randint

root = Tk()
root.title('Domande')
root.geometry("850x400")
root.resizable(True, True)

words = [
    (("Stadio Olimpico"), ("ROMA")),
    (("Stadio Artemio Franchi"), ("FIORENTINA")),
    (("Stadio Armando Picchi"), ("LIVORNO")),
    (("Stadio Luigi Ferraris"), ("SAMPDORIA"))
    (("Stadio Arechi"), ("SALERNITANA"))
]

# get a count of our wods list
count = len(words)

def next():
    global hinter, hint_count
    #Clear screen
    answer_label.config(text="")
    label.config(text="")
    hint_label.config(text="")
    #Reset Hint stuff
    hinter = ""
    hint_count = 0

    #Create random selection
    global random_domande
    random_domande = randint(0, count-1)
    #Update label with Domande
    domande.config(text=words[random_domande][0])


#Keep track of the hint
hinter = ""
hint_count = 0

def hint():
    global hint_count
    global hinter

    if hint_count < len(words[random_domande][1]):
        hinter = hinter + words[random_domande][1][hint_count]
        hint_label.config(text=hinter)
        hint_count +=1

#Define function to hide the widget
def hide_widget(widget):
    widget.pack_forget()
    widget.config(text=words[random_domande][1])

#Define a function to show the widget
def show_widget(widget):
    widget.pack()
    widget.config(text=words[random_domande][1])

#Labels
domande = Label(root, text="", font=("Times", 14))
domande.pack(side=TOP)

#Create an Label Widget
label = Label(root, text="", font=("Times", 14))
label.pack()

#Create Buttons
button_frame = Frame(root)
button_frame.pack()

hint_button = Button(button_frame, text="Hint", command=hint)
hint_button.pack()

button_show = Button(root, text= "Show", command= lambda:show_widget(label))
button_show.pack()

#Create a button Widget
button_hide = Button(root, text= "Hide", command=lambda:hide_widget(label))
button_hide.pack()

next_button = Button(button_frame, text="Next", command=next)
next_button.pack()

#Create Hint Label
hint_label = Label(root, text="")
hint_label.pack()

answer_label = Label(root, text="", font=("Times", 14))
answer_label.pack(side=BOTTOM)
#Run next function when the program starts
next()

root.mainloop()



Aucun commentaire:

Enregistrer un commentaire