turtlesXD
turtlesXD

Reputation: 57

Using random.choice() to generate multiple times, then assign to a variable

I am trying to make a game , and one of the functions needed is random text generation(From a Dict). For this I've been using the the random.choice() function with the random library. I want the code to generate a new result everytime the function is called. Here is the code:

colours = ["red","yellow","orange","green","blue","purple","grey"]
words = ["red","yellow","orange","green","blue","purple","grey"]
worrd = random.choice(words)
def get():
 print(ent.get())


def start():
 print("started")
 lab.pack_forget()
 sta.pack_forget()
 opt.pack(padx=5,pady=4)
 ent.pack(padx=5,pady=9)
 acc.pack(padx=7,pady=9)
 opt.config(text=worrd,fg=random.choice(colours))

How would I generate a new result from the words dict, and then assign it to the worrd variable?

Upvotes: 0

Views: 169

Answers (1)

Nissan Apollo
Nissan Apollo

Reputation: 65

You can use random index:

import random
words = ["red","yellow","orange","green","blue","purple","grey"]
worrd=words[random.randint(0,len(words)-1)]

It's change worrd variable every time.

Upvotes: 1

Related Questions