Tkinter版字母数游戏答案与问题不匹配,求排查
字母数量猜词游戏问题说明与解决
游戏规则
这是类似颜色干扰游戏的玩法,规则是说出单词的字母数量对应的英文单词,而非单词本身,例如:
- five(5个字母)对应four
- six(3个字母)对应three
问题现象
Linux Mint 21下的终端版本代码可正常运行,但Tkinter图形界面版本中出现答案与问题不匹配的错误。
问题代码片段(Tkinter版本)
while True: word = random.choice(words) label1.config(text=numbers[word]) button_pressed = StringVar() button = Button( root, bg="#F20505", fg="White", activebackground="yellow", activeforeground="black", width=10, height=5, relief=RAISED, text="Enter", command=lambda: button_pressed.set("button pressed"), ) button.grid(row=2, column=0) button.wait_variable(button_pressed) button.destroy() guess = box.get() if guess == numbers[word]: label3.config(text="Correct!", bg="yellow", fg="green") num_right += 1 scoreLabel = Label( root, text=f"Your score is {num_right}", width="25", height="5", bg="#0C00FF", fg="white", font="Arial, 24", ) scoreLabel.grid(row=1, column=1) box.delete(0, END) elif guess.strip().lower() == "exit": sys.exit() elif guess.strip().lower() == "end": label3.config(bg="yellow", fg="black", text=result()) else: label3.config(text="Incorrect", bg="yellow", fg="red") num_wrong += 1 box.delete(0, END)
可正常运行的终端版本代码(Linux Mint 21)
import random import sys numbers = { "four": "four", "five": "four", "six": "three", "seven": "five", "eight": "five", "nine": "four", "ten": "three", } words = tuple(numbers) while True: word = random.choice(words) ans = input(f"{word.title()}?\n").strip().lower() if ans == numbers[word]: print("Correct") elif ans.strip().lower() == "exit": sys.exit() else: print("Correct answer is:", numbers[word])
问题解决说明
此前对代码逻辑存在理解误区,经jasonharper的建议已解决问题,在此感谢jasonharper及EvensF。
内容的提问来源于stack exchange,提问作者WindowLicker
相关产品推荐
相关产品推荐

