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
添加回答
舉報