程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Word cookie game Python ,为什么即使它存在于文件中,它也不计算我的字数?大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决Word cookie game Python ,为什么即使它存在于文件中,它也不计算我的字数??

开发过程中遇到Word cookie game Python ,为什么即使它存在于文件中,它也不计算我的字数?的问题如何解决?下面主要结合日常开发的经验,给出你关于Word cookie game Python ,为什么即使它存在于文件中,它也不计算我的字数?的解决方法建议,希望对你解决Word cookie game Python ,为什么即使它存在于文件中,它也不计算我的字数?有所启发或帮助;

我正在开发一款文字饼干游戏。我有一个包含 429k 英文单词的文件,但是当我输入我的单词时,它不计算它们不知道为什么。代码在 Python 下面

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 在任何一种情况下都有返回值,并且由于在循环的每次迭代中总是评估一个或另一个,因此可以保证您会过早地退出函数。 >

您可能想要的是摆脱 else - 只检查是否成功,如果在任何情况下都不能保证成功,then 声明失败。

第二次返回似乎没有必要,特别是因为它返回的类型(布尔值)与分数 (int) 不同,所以我只想摆脱它。

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
# after the for loop finishes we can safely say the word
# was not found in the file.
print("that's not a word start the game again")
game_on = false

大佬总结

以上是大佬教程为你收集整理的Word cookie game Python ,为什么即使它存在于文件中,它也不计算我的字数?全部内容,希望文章能够帮你解决Word cookie game Python ,为什么即使它存在于文件中,它也不计算我的字数?所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。