我有一个程序,用于从列表中选择单词并将其显示在标签上并将其删除。
每当用户正确输入单词时,他们的word_count
将上升1,如果他们错了,则会下降1。
import random
import tkinter
words = ["Games","Development","Keyboard","Speed","Typer","Anything","Alpha","Zealous","Accurate","Basics","Shortcut","Purpose","Window","Counter","Fortress","Modification","Computer","Science","History","Football","Basketball","Solid","Phantom","Battlefield","Advanced","Warfare","Download","Upload","Antidisestablishmentarianism","Supercalifragilisticexpialidocious","Discombobulation","Liberated","Assassin","Brotherhood","Revelation","Unity","Syndicate","Victory"]
word_count = 1
def nextWord():
global word_count
entry.focus_set()
while word_count <= 12:
random_word = random.choice(words)
label.config(text=str(random_word))
entry.delete(0, tkinter.END)
word_counter.config(text="Words: " + str(word_count))
if entry.get().lower() == random_word.lower():
entry.delete(0, tkinter.END)
word_count += 1
word_counter.config(text="Words: " + str(word_count))
wrong_label.config(text="")
words.remove(random_word)
elif entry.get().lower() != random_word.lower():
entry.delete(0, tkinter.END)
wrong_label.config(text="WRONG!")
word_count -= 1
word_counter.config(text="Words: " + str(word_count))
break
def startGame(event):
nextWord()
window = tkinter.Tk()
window.title("Fastest Typer")
label = tkinter.Label(window, font=("Helvetica", 60))
word_counter = tkinter.Label(window, text="Words: " + str(word_count), font =("Helvetica", 14))
wrong_label = tkinter.Label(window, text="", font =("Helvetica, 14"))
entry = tkinter.Entry(window)
window.bind("<Return>", startGame)
label.pack()
word_counter.pack()
wrong_label.pack()
entry.pack()
entry.focus_set()
window.mainloop()
我的问题是:当运行上述程序并正确输入单词时,wordcounter
显示-1而不是1和-2而不是2等。此外, wrong_label
的文字在正确的时候不会改变。
更新
我尝试按以下方式重新排序代码,但现在word_count
根本不会增加或减少:
import random
import tkinter
words = ["Games","Development","Keyboard","Speed","Typer","Anything","Alpha","Zealous","Accurate","Basics","Shortcut","Purpose","Window","Counter","Fortress","Modification","Computer","Science","History","Football","Basketball","Solid","Phantom","Battlefield","Advanced","Warfare","Download","Upload","Antidisestablishmentarianism","Supercalifragilisticexpialidocious","Discombobulation","Liberated","Assassin","Brotherhood","Revelation","Unity","Syndicate","Victory"]
word_count = 1
def nextWord(event):
global word_count
entry.focus_set()
while word_count <= 12:
random_word = random.choice(words)
entry.delete(0, tkinter.END)
label.config(text=str(random_word))
words.remove(random_word)
return
if entry.get().lower() == random_word.lower():
entry.delete(0, tkinter.END)
word_count += 1
word_counter.config(text="Words: " + str(word_count))
wrong_label.config(text="")
elif entry.get().lower() != random_word.lower():
entry.delete(0, tkinter.END)
wrong_label.config(text="WRONG!")
word_count -= 1
word_counter.config(text="Words: " + str(word_count))
entry.delete(0, tkinter.END)
def startGame(event):
nextWord()
window = tkinter.Tk()
window.title("Fastest Typer")
frame = tkinter.Frame(window)
label = tkinter.Label(frame, font=("Helvetica", 60))
word_counter = tkinter.Label(frame, text="Words: 0", font =("Helvetica", 14))
wrong_label = tkinter.Label(frame, text="", font =("Helvetica, 14"))
entry = tkinter.Entry(frame)
window.bind("<Return>", startGame)
label.pack()
word_counter.pack()
wrong_label.pack()
entry.pack()
frame.pack()
entry.focus_set()
window.mainloop()
答案 0 :(得分:0)
由于您的代码中存在逻辑错误,因此从不获得正确的字词。每当您按Enter键时,都会调用startGame
,然后调用nextWord
。 nextWord
功能然后生成一个新的随机词,将其与您当前键入的内容进行比较,在屏幕上显示新词。