Word cookies游戏Python,为什么它';即使文件里有我的话,他也不算吗?
我正在做一个单词饼干游戏。我有一个429k英语单词的文件,但当我输入单词时,它不计算它们,不知道为什么。下面是Python中的代码Word cookies游戏Python,为什么它';即使文件里有我的话,他也不算吗?,python,file,Python,File,我正在做一个单词饼干游戏。我有一个429k英语单词的文件,但当我输入单词时,它不计算它们,不知道为什么。下面是Python中的代码 import random from random import shuffle letters = ["c", "a", "e", "r", "u", "o", "i", "n", "b&quo
import random
from random import shuffle
letters = ["c", "a", "e", "r", "u", "o", "i", "n", "b"]
score = 0
game_on = True
f = open(r"C:\Users\MSI\Desktop\wordlist.txt", "r")
def easy(letter_list, file):
global score
global game_on
# unique_letters = [i for i in letter_list]
# mode_letters = random.sample(unique_letters, 3)
shuffle(letter_list)
mode_letters = letter_list[:3]
print(mode_letters)
guess_word = input("combine the letters to form a word")
for line in file:
if guess_word in line:
score += 1
print("correct word you get a +1 to your score")
print(score)
return score
else:
print(" that's not a word start the game again")
game_on = False
return game_on
while game_on:
print("hello welcome to word cookies game")
difficulty = input("choose what difficulty you would like to have Easy / Medium / difficult").lower()
if difficulty == "easy":
easy(letters, f)
尝试添加以下内容或类似内容:
file = open('File', 'r+')
words = file.readlines()
for word in words:
words.replace(word, word.strip()) # To remove all the '\n' newline characters
if guess_word in words:
score += 1
假设每行上都有一个不同的单词,这应该可以正常工作。
return
中断函数,而不仅仅是循环。
在任何一种情况下,if/else都有一个返回,并且由于每次循环迭代都会对其中一个进行求值,因此可以保证您会过早地中断函数
您可能想要的是摆脱“仅限其他”的成功检查,当任何情况下都不能保证成功时,则声明失败
第二个返回似乎是不必要的,特别是因为它返回的类型(布尔值)与score(int)不同,所以我只想去掉它
对于文件中的行:
如果猜一行字:
分数+=1
打印(“正确的单词,你的分数为+1”)
打印(分数)
回击得分
#在for循环完成后,我们可以放心地说出这个词
#在文件中找不到。
打印(“这不是一个词,重新开始游戏”)
博弈论=错误
一个问题是,easy
在第一次迭代时返回,因此它只能看到文件中的第一行。这不是很好吗?由于它将在while循环标志仍在运行时再次运行?@Azizalfarhan No,它将从函数返回。换句话说,它不会继续循环。Return意味着退出函数并返回调用它的位置。欢迎使用StackOverflow!尝试在你的代码中添加注释,以帮助其他人理解你试图实现的目标。回答得很好