2 回答

TA贡献1883条经验 获得超3个赞
您没有检查字母表中的每个字母。您只检查单词是否包含字母 。让我们检查控制流a
def is_pangram(x):
x = x.lower()
alphabet="abcdefghijklmnopqrstuvwxyz"
# we begin iteration here
for i in alphabet:
# we check if the letter is in the word
if i in x:
# we return! This will stop execution at the first True
# which isn't always the case
return True
else:
# we return again! since we've covered all cases, you will
# only be checking for a
return False
要解决此问题,您可以执行以下两项操作之一。使用循环,您可以只检查字母是否不在 中,如果字母不在,则返回,并在末尾返回:xFalseTrue
def is_pangram(x):
x = x.lower()
alphabet="abcdefghijklmnopqrstuvwxyz"
for i in alphabet:
if i not in x:
return False
# this means we made it through the loop with no breaks
return True
或者,您可以使用运算符检查字母表中的所有字母是否都在单词中,该单词返回 ,否则返回allTrueFalse
def is_pangram(x):
x = x.lower()
alphabet="abcdefghijklmnopqrstuvwxyz"
return all(i in x for i in alphabet)

TA贡献1876条经验 获得超6个赞
对于包含任何字母的任何字符串,第一个函数将返回 true。您需要验证每个字母是否都在输入字符串中:
import string
def ispangram(x):
x = x.lower()
for c in string.ascii_lowercase:
if c not in x:
# a letter is not in the input string, so the input string is not a pangram
return False
# if we got here, each letter is in the input string
return True
添加回答
举报