生成一段可以运行的有意思的代码
这是一个简单的 Python 代码,可以随机生成一句话,并检查这句话中是否包含特定的单词:
import random
# 随机生成一句话
sentences = ["The quick brown fox jumps over the lazy dog.",
"I am the walrus.",
"To be or not to be, that is the question.",
"Hello world!",
"Life is like a box of chocolates, you never know what you're gonna get."]
sentence = random.choice(sentences)
# 检查句子中是否包含特定的单词
words_to_check = ["fox", "walrus", "chocolates", "banana"]
for word in words_to_check:
if word in sentence:
print(f"The sentence '{sentence}' contains the word '{word}'.")
else:
print(f"The sentence '{sentence}' does not contain the word '{word}'.")
运行结果可能如下:
The sentence 'I am the walrus.' contains the word 'walrus'.
The sentence 'I am the walrus.' does not contain the word 'fox'.
The sentence 'I am the walrus.' does not contain the word 'chocolates'.
The sentence 'I am the walrus.' does not contain the word 'banana'.
``
原文地址: https://www.cveoy.top/t/topic/cZum 著作权归作者所有。请勿转载和采集!