I am trying to create a jumbled word but the right amount of letters are not showing when running the code. There’s only 1 letter being displayed and if the user actually inputs the right answer, the code only takes it as a wrong answer. Is there a way to fix this problem? Thank you!
import tkinter from tkinter import * import random from tkinter import messagebox from random import shuffle answer =["python", "youtube","google","samosa","india","tea","coffee"] words= [] for i in answer: words= list(i) shuffle(words) words.append(words) num= random.randint(0,len(words)) def initial(): global words,answer,num lbl1.configure(text=words[num]) def ans_check(): print(" ") global words,num,answer user_input= e1.get() if user_input==answer[num]: messagebox.showinfo("Success","yup, this is right!") Reset() else: messagebox.showinfo("Error","Make sure it's without capitals") e1.delete(0,END) def Reset(): global words,num,answer num= random.randint(0,len(words)) lbl1.configure(text=words[num]) e1.delete(0,END) root= Tk() root.geometry('300x300') lbl1= Label(root,font='times 20') lbl1.pack(pady=30,ipady=10,ipadx=10) answer12= StringVar() e1= Entry(root,textvariable=answer) e1.pack(ipady=5,ipadx=5) bt1= Button(root,text='Check',width=20,command=ans_check) bt1.pack(pady=40) bt2= Button(root,text='Reset',width=20,command=Reset) bt2.pack() initial() root.mainloop()
Advertisement
Answer
O lord how many problems this has :). Dont worry!
First of all, your biggest problem is you created a list called words
to contain a list of shuffled words. Then you create the same variable called words
which stores list(word)
. This messes up your program entirely.
Also, if your user is incorrect, tell them they got it wrong. Dont show an error for capitalization. To avoid this, you can use .lower()
Code
import tkinter from tkinter import * import random from tkinter import messagebox from random import shuffle answer =["python", "youtube","google","samosa","india","tea","coffee"] words_lst= [] for i in answer: word = list(i) shuffle(word) words_lst.append(word) num= random.randint(0,len(words_lst)) def initial(): global words,answer,num lbl1.configure(text=answer[num]) def ans_check(): print(" ") global words,num,answer user_input= e1.get() if user_input.lower()==answer[num]: messagebox.showinfo("Success","yup, this is right!") Reset() else: messagebox.showinfo("Error","Incorrect") e1.delete(0,END) def Reset(): global words,num,answer num= random.randint(0,len(words_lst)) lbl1.configure(text=words_lst[num]) e1.delete(0,END) root= Tk() root.geometry('300x300') lbl1= Label(root,font='times 20') lbl1.pack(pady=30,ipady=10,ipadx=10) answer12= StringVar() e1= Entry(root,textvariable=answer) e1.pack(ipady=5,ipadx=5) bt1= Button(root,text='Check',width=20,command=ans_check) bt1.pack(pady=40) bt2= Button(root,text='Reset',width=20,command=Reset) bt2.pack() initial() root.mainloop()