Skip to content
Advertisement

How do I update a variable that is pulling randomly from a list using python?

I am trying to make a small flash card program for my kids. Here is what I have come up with so far, I was hoping if I reassigned the variables in the button function it would randomly pull a new list item. But that does not seem to be the case. I know my code is not he prettiest, I’ve been teaching myself how to code for about 3 weeks now, so if there is anything else anyone can recommend to help improve this I would appreciate it.

import tkinter as tk
import random
window = tk.Tk()
window.geometry("300x600")
window.title("Flash Card Master")

x = [1, 2, 3, 4, 5, 6, 7, 8, 9 ];
y = [1, 2, 3, 4, 5, 6, 7, 8, 9 ];
a = ''
plus = '+'
rx = random.choice(x)
ry = random.choice(y)

top = tk.Label(text = rx, font="Georgia 100 bold")
operator = tk.Label(text = plus, font="Georgia 100 bold")
bottom = tk.Label(text = ry, font="Georgia 100 bold")
slash = tk.Label(text = '    ____________________', font="Georgia 10 bold")
answere = tk.Label(text = '', font="Georgia 100 bold")

top.grid(row=0, column=1)
operator.grid(row=1, column=0)
bottom.grid(row=1, column=1)
slash.grid(row=2, column=0, columnspan=3)
answere.grid(row=3, column=1)

def press():
    answere.config(text = rx + ry)
b1 = tk.Button(text = "Click ME!", command = press)
b1.grid()

def press():
    answere.config(text = a)
    rx = random.choice(x)
    ry = random.choice(y)
    top = tk.Label(text = rx, font="Georgia 100 bold")
    operator = tk.Label(text = plus, font="Georgia 100 bold")
    bottom = tk.Label(text = ry, font="Georgia 100 bold")
b1 = tk.Button(text = "Next", command = press)
b1.grid()

window.mainloop()

Advertisement

Answer

There were two problems that were stopping your code from working, so I’ve listed them below.

Firstly, your ‘rx’ and ‘ry’ variables inside your “press” function for generating a new flash card were local, which means they only worked inside the function and didn’t change the values outside of the function. I put the ‘rx’ and ‘ry’ variables you created at the top inside an array so you can alter them inside a function and have the changes stay.

Secondly, there was a small error inside your second “press” function where you didn’t use “config()” on “top”, “operator”, or “bottom”. I fixed that.

import tkinter as tk
import random
window = tk.Tk()
window.geometry("300x600")
window.title("Flash Card Master")

x = [1, 2, 3, 4, 5, 6, 7, 8, 9 ]
y = [1, 2, 3, 4, 5, 6, 7, 8, 9 ]
a = ''
plus = '+'
rx = random.choice(x)
ry = random.choice(y)
random_vars = [rx, ry]

top = tk.Label(text = rx, font="Georgia 100 bold")
operator = tk.Label(text = plus, font="Georgia 100 bold")
bottom = tk.Label(text = ry, font="Georgia 100 bold")
slash = tk.Label(text = '    ____________________', font="Georgia 10 bold")
answere = tk.Label(text = '', font="Georgia 100 bold")

top.grid(row=0, column=1)
operator.grid(row=1, column=0)
bottom.grid(row=1, column=1)
slash.grid(row=2, column=0, columnspan=3)
answere.grid(row=3, column=1)


def press_one():
    answere.config(text = random_vars[0] + random_vars[1])


b1 = tk.Button(text = "Click ME!", command = press_one)
b1.grid()


def press_two():
    answere.config(text = a)
    random_vars[0] = random.choice(x)
    random_vars[1] = random.choice(y)
    top.config(text = random_vars[0], font="Georgia 100 bold")
    operator.config(text = plus, font="Georgia 100 bold")
    bottom.config(text = random_vars[1], font="Georgia 100 bold")


b1 = tk.Button(text = "Next", command = press_two)
b1.grid()

window.mainloop()
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement