【发布时间】:2018-11-10 22:46:34
【问题描述】:
我想根据传递的参数从文件中返回随机单词。但如果参数不匹配任何我不想返回任何东西。我的方法如下:
def word_from_score(self,score):
print(random.choices([word for word in self.file if sum([LETTER_SCORES[letter] for letter in word ]) == score]))
它根据命令行中传递的参数从文件中返回正确的单词,但如果参数不匹配,我不想返回任何内容,例如''。我怎么能在这个语句中添加其他内容?
【问题讨论】:
-
请提供minimal reproducible example 示例输入和预期输出。