[英]FOR loop in function exists before ending
I know there are already countless clones of Wordle.我知道已经有无数的 Wordle 克隆。 Nevertheless I try to program my own version.
尽管如此,我还是尝试编写自己的版本。 In the function is_real_word it should be checked whether the entered word of the user occurs in the word list.
在函数 is_real_word 中,应该检查用户输入的单词是否出现在单词列表中。 If so, the variable check = True.
如果是,则变量 check = True。 However, the FOR loop is always exited when the counter is at 1. The file "5_letter_words.txt" contains 3 entries: wetter, wolle, watte And last but not least also the return value for eingabewort is sometimes NONE.
但是,当计数器为 1 时,FOR 循环总是退出。文件“5_letter_words.txt”包含 3 个条目:wetter、wolle、watte 最后但并非最不重要的一点是,eingawort 的返回值有时为 NONE。 And I don't know why?
我不知道为什么?
import random
def word_list():
wordle = []
with open("5_letter_words.txt", "r") as file:
for line in file:
myTuple = line.strip()
wordle.append(myTuple)
return wordle
def random_word(wordlist):
return random.choice(wordlist)
def is_real_word(guess, wordlist):
for word in wordlist:
if guess == word:
return True
return False
def check_guess(guess, randomword):
randomword_tuple = []
guess_tuple = []
length = len(randomword)
output = ["-"] * length
for index in range(length):
if guess[index] == randomword[index]:
output[index] = "X"
randomword = randomword.replace(guess[index], "-", 1)
for index in range(length):
if guess[index] in randomword and output[index] == "-":
output[index] = "O"
randomword = randomword.replace(guess[index], "-", 1)
return ''.join(output)
def next_guess(wordlist):
guess = input('Please enter a guess: ')
guess = guess.lower()
valid = is_real_word(guess, wordlist)
if valid == False:
print('Thats not a real word!')
next_guess(wordlist)
else:
return guess
def play():
target = []
target = word_list()
zufallswort = str()
zufallswort = random_word(target)
eingabewort = next_guess(target)
print('Eingabewort: ', eingabewort)
print('Zielwort: ', zufallswort)
play()
Could you check if your function word_list returns the list?你能检查你的函数 word_list 是否返回列表吗? You dont give a clear path, only the file name.
你没有给出明确的路径,只有文件名。
If it works, try:如果有效,请尝试:
def is_real_word(guess, wordlist):
for word in wordlist:
if guess == word:
check = 1
break
return check
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.