python - 如何使 TypeError:'NoneType' 类型的对象没有 len() 与刽子手游戏消失
问题描述
import random
hangmanGame = True
while hangmanGame:
def wordsPickedBank():
with open('words.txt', 'r') as file:
allText = file.read()
words = list(map(str, allText.split()))
wordsPickedBank(random.choice(words))
wordPicked = wordsPickedBank()
word_length = len(wordPicked)
guesses = str.split("")
** 第 14 行出现错误,您能帮忙完成这项工作吗?我正在尝试使单词显示为“_”,并从文本文件中随机选择单词。你能展示如何解决这个问题,以便循环继续而不向用户显示单词吗?**
解决方案
按如下方式编辑您的函数:
while hangmanGame:
def wordsPickedBank():
with open('words.txt', 'r') as file:
allText = file.read()
words = list(map(str, allText.split()))
return random.choice(words)
要返回一个变量,你必须使用return
关键字,而不是函数本身。
推荐阅读
- excel - 计算数量,不计算重复
- sql - SQL - 数据库引用
- python - Python:如何解析这个列表?
- javascript - 如何在完成选择项目之前停止关闭 Kendo MultiColumnDropDown?
- java - 简单的 ArrayList.remove(int) 不起作用。我做错了什么?
- python - MongoEngine python库过滤器查询获取本机mongo语法
- laravel - 决赛桌的雄辩模型关系
- pandas - pandas:px.parallel_coordinates,如何调整每个轴的间隔?
- c++ - 警告 LNK4099:找不到 PDB 'vc110.pdb'
- python - 按名称重新定义 python 函数的默认参数