这是我使用代码遇到的新错误。它是带有GUI和Word文件的屏幕截图。
图像是我在盒子中输入键的图像,它并没有表明它给我不正确,但是后来它给了我不正确的。短时间显示不正确,因此无法在屏幕截图中捕获。我试图在python中制作一个游戏,用户在gui中输入一个单词(tkinter),该单词必须在txt文件中(包含10,000个)常见的英语单词)和输入的字母必须以随机生成的字母序列为单词。
问题是,每次我输入一个单词时,都说这是一个单词。我发现这个词必须与随机生成的字母序列中的顺序相同,否则它说单词不正确。有人可以帮我解决这个问题吗?我不知道如何解决:)P.S我的代码非常混乱,check_in_sequence函数只是我尝试的检查处理的一部分。
import random
from Tkinter import *
root = Tk()
Answer = Entry(root)
alphabet = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
points = 0
words = []
used = []
random_letter_sequence = ' '
results = []
def check_in_sequence():
global random_letter_sequence
correctletters = 0
listanswer = list(Answer.get().lower())
for letter in listanswer :
if letter in random_letter_sequence:
correctletters += 1
if correctletters == len(listanswer):
return True
else:
return False
with open("This IS.txt") as f:
content = f.readlines()
filecontent = [x.strip() for x in content]
def generate_sequence():
global random_letter_sequence
for random_letter in range(random.randint(6, 10)):
random_letter = random.choice(alphabet)
random_letter_sequence = random_letter_sequence + random_letter
def this_delete():
Incorrect_Label.pack_forget()
def checking_answer():
with open('This IS.txt') as inputfile:
global file_contents
file_contents = inputfile.read()
file_contents = file_contents.strip("\n")
if Answer.get().lower() in random_letter_sequence and file_contents:
global points
print(Answer.get())
points += 1
PointLabel = Label(root, text=points, font='Helvetica 48')
PointLabel.config(text=points)
PointLabel.pack()
else:
global Incorrect_Label
Incorrect_Label = Label(text='Incorrect')
Incorrect_Label.pack()
Incorrect_Label.after(1500, this_delete)
print(Answer.get())
Answer.delete(0, END)
def enter_click(event):
checking_answer()
generate_sequence()
check = Answer.get()
trial_dontNeedALabel = Label(text=random_letter_sequence,
font='Helvetica 48')
Check_Button = Button(root, text='Submit', command=checking_answer)
root.bind('<Return>', enter_click)
Check_Button.pack()
Answer.pack()
trial_dontNeedALabel.pack()
root.mainloop()
这是您要做的事情(您没有说重复很重要,如果他们这样做,我使用Collection的计数器来解决这个问题,否则情况更为直接)?
from __future__ import print_function
from collections import Counter
def test_word(w):
"""return True if the word is in the list accepted_words and the word is made of letters in random_sequence (accounting for repetition). Return False otherwise. """
return (w in accepted_words) and Counter(random_sequence)&Counter(w)==Counter(w)
#test example
accepted_words=['dog','cat', 'fish','racoon']
random_sequence='ladfaractsigo'
#print test results
print (test_word('dog')) #valid -> True
print (test_word('elephant')) #not in sequence of letters nor in accepted words
print (test_word('fish')) #good word, but not in letters
print (test_word('fada')) #in letters, but not a valid word
print (test_word('racoon')) #all letters are valid, but 'o' is used twice -> False
检查单词是否在文本文件中并且在Random_text_string中,将整个文件读为字符串,然后do
if word in text_file_string and word in random_text_string:
#do stuff
评论后编辑
通过输入字符串循环,并检查每个字符是否在随机_String中
def is_input_string_good(input_string, random_string):
for character in input_string:
if character not in random_string:
return False
return True
编辑:
弄乱了您的代码后,我不得不恢复一些功能,因此我可以包括错误检查并正确匹配字符串。
代码的主要更改是首先检查答案中的所有字母是否均在可用的随机生成的信件中。如果没有,它将说不正确,并且break
序列,因此它停在那里。然后,如果所有字母都匹配,那么您可以查看文件中是否存在单词。我们以此顺序这样做的原因是要缩短阅读大量文件的时间至少。
我还用来自包含所有可用英文字母的string
库中的构建替换了您的字母列表。
import random
from string import ascii_letters # new import and got rid of your list of letters
from tkinter import *
from pip.utils import file_contents
root = Tk()
Answer = Entry(root)
points = 0
words = []
used = []
random_letter_sequence = ''
results = []
PointLabel = Label(root, font='Helvetica 48')
def generate_sequence():
global random_letter_sequence
# this for loop was indented one section to far.
for random_letter in range(random.randint(6, 10)):
random_letter = random.choice(ascii_letters).upper()
random_letter_sequence += random_letter
def incorrect_submission():
Incorrect_Label = Label(root, text='Incorrect')
Incorrect_Label.pack()
Incorrect_Label.after(1500, lambda: Incorrect_Label.pack_forget())
def checking_answer():
global points
match_alphabet = False # used as a tracker for the letters in answer
word_match = False # used as a tracker if answer in the file
# Change the file path for your file
with open('./This_IS.txt') as inputfile:
for word in inputfile.read().split("n"):
file_contents = []
file_contents.append(word)
# first check to make sure the answer is not empty
if Answer.get() != "":
match_alphabet = True
# check if all letters in the answer are in the available list.
for letter in Answer.get().lower():
if letter not in random_letter_sequence.lower():
match_alphabet = False
# if everything above passes fine then check the file for the word in the answer
if match_alphabet == True:
for word in file_contents:
if Answer.get().lower() == word.lower():
points += 1
PointLabel.config(text=points)
PointLabel.pack()
Answer.delete(0, END)
word_match = True
if word_match == False:
incorrect_submission()
def enter_click(event):
checking_answer()
generate_sequence()
check = Answer.get()
trial_dontNeedALabel = Label(text=random_letter_sequence,
font='Helvetica 48')
Check_Button = Button(root, text='Submit', command=checking_answer)
root.bind('<Return>', enter_click)
Check_Button.pack()
Answer.pack()
trial_dontNeedALabel.pack()
root.mainloop()
请记住,如果您要保持在拼字公式的真实本质中。