The image is of me typing key into the box and it doesnt show that it gives me incorrect, but afterwards it gave me incorrect. Incorrect is displayed for a short time so wasnt able to capture in screenshot.我试图在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()
答案 0 :(得分:1)
这是你想要做的事情(你没有说重复是否重要,如果他们这样做,我使用Counter从收集来解释,否则案件更简单)?
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
答案 1 :(得分:0)
要检查单词是否在文本文件中且位于random_text_string中,请将整个文件作为字符串读取,然后执行
if word in text_file_string and word in random_text_string:
#do stuff
评论后编辑
循环输入字符串并检查每个字符是否在random_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
答案 2 :(得分:0)
编辑:
稍微搞乱你的代码后,我不得不重做一些函数,所以我可以包含错误检查并正确匹配字符串。
代码的主要变化是首先检查答案中的所有字母是否都是随机生成的字母。如果不是,它会说不正确的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()
请记住,如果你试图保持拼字游戏公式的真实性质,你需要提供一些检查,以检查一个字母是否已经用一个单词中的随机可用字母用重复字母