不正确比较列表

huangapple go评论62阅读模式
英文:

Doesnt compare list's correctly

问题

def split(word):
    x = list(word)
    return x

whitelist = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P']

word = input('Password ')
split(word)
x = split(word)

if not all(letter in whitelist for letter in x):
    print('Incorrect letters')
else: 
    print('Correct')
英文:
def split(word):
    x = list(word)
    return x



whitelist = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P']




word = input('Password ')
split(word)
x = split(word)

if x not in whitelist:
    print('Incorrect letters')
else: 
    print('Correct ')

When i enter FECHCH for eks. , 'if' keeps giving me communicate - inncorrect letters .
I need python3 to understand, that letters FECHCH are in whitelist.

答案1

得分: 0

您需要逐个输入字母

def split(word):
    x = list(word)
    return x

whitelist = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P']

word = input('密码 ')
split(word)
x = split(word)
print(x)

# 密码 FECHCH
['F', 'E', 'C', 'H', 'C', 'H']

所以您正在比较:

['F', 'E', 'C', 'H', 'C', 'H'] 在 ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P']

这是 False

正如评论中所提到的,您可以使用 set() 来去除重复并检查子集。

word = input('密码 ')
split(word)
x = split(word)

if set(x).issubset(whitelist):
    print('正确 ')
    
else: 
    print('不正确的字母')
英文:

You need to enter letters one by one

def split(word):
    x = list(word)
    return x

whitelist = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P']

word = input('Password ')
split(word)
x = split(word)
print(x)

#Password FECHCH
['F', 'E', 'C', 'H', 'C', 'H']

so you are comparing:

['F', 'E', 'C', 'H', 'C', 'H'] in ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P']

which is False

as mentioned in comments, you can use a set() to remove duplicates and check subsets.

word = input('Password ')
split(word)
x = split(word)

if set(x).issubset(whitelist):
    print('Correct ')
    
else: 
    print('Incorrect letters')

答案2

得分: 0

无需特殊的拆分函数。您可以简单地使用(假设您接受小写字母要被转换,否则省略upper()函数):

whitelist = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P']

word = input('密码 ')

# 对每个字符进行报告
for ch in word.upper():
    if ch not in whitelist:
        print('不正确的字母', ch)
    else:
        print('正确的字母', ch)

# 或者检查整个单词
for ch in word.upper():
    if ch not in whitelist:
        val = '不正确的字母'
        break
else:
    val = '正确的字母'
print(val)
英文:

No special split function is required. You could simply use (assuming you accept lower case letters to be converted else omit upper():

whitelist = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P']

word = input('Password ')

# to report on each character   

for ch in word.upper():
    if ch not in whitelist:
        print('Incorrect letter', ch)
    else: 
        print('Correct letter', ch)

# or to check the whole word

for ch in word.upper():
    if ch not in whitelist:
        val = 'Incorrect letters'
        break
else:
    val = 'correct letters'
print(val)

huangapple
  • 本文由 发表于 2023年2月8日 22:43:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75387419.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定