不正确比较列表

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

Doesnt compare list's correctly

问题

  1. def split(word):
  2. x = list(word)
  3. return x
  4. whitelist = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P']
  5. word = input('Password ')
  6. split(word)
  7. x = split(word)
  8. if not all(letter in whitelist for letter in x):
  9. print('Incorrect letters')
  10. else:
  11. print('Correct')
英文:
  1. def split(word):
  2. x = list(word)
  3. return x
  4. whitelist = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P']
  5. word = input('Password ')
  6. split(word)
  7. x = split(word)
  8. if x not in whitelist:
  9. print('Incorrect letters')
  10. else:
  11. 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

您需要逐个输入字母

  1. def split(word):
  2. x = list(word)
  3. return x
  4. whitelist = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P']
  5. word = input('密码 ')
  6. split(word)
  7. x = split(word)
  8. print(x)
  9. # 密码 FECHCH
  10. ['F', 'E', 'C', 'H', 'C', 'H']

所以您正在比较:

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

这是 False

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

  1. word = input('密码 ')
  2. split(word)
  3. x = split(word)
  4. if set(x).issubset(whitelist):
  5. print('正确 ')
  6. else:
  7. print('不正确的字母')
英文:

You need to enter letters one by one

  1. def split(word):
  2. x = list(word)
  3. return x
  4. whitelist = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P']
  5. word = input('Password ')
  6. split(word)
  7. x = split(word)
  8. print(x)
  9. #Password FECHCH
  10. ['F', 'E', 'C', 'H', 'C', 'H']

so you are comparing:

  1. ['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.

  1. word = input('Password ')
  2. split(word)
  3. x = split(word)
  4. if set(x).issubset(whitelist):
  5. print('Correct ')
  6. else:
  7. print('Incorrect letters')

答案2

得分: 0

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

  1. whitelist = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P']
  2. word = input('密码 ')
  3. # 对每个字符进行报告
  4. for ch in word.upper():
  5. if ch not in whitelist:
  6. print('不正确的字母', ch)
  7. else:
  8. print('正确的字母', ch)
  9. # 或者检查整个单词
  10. for ch in word.upper():
  11. if ch not in whitelist:
  12. val = '不正确的字母'
  13. break
  14. else:
  15. val = '正确的字母'
  16. print(val)
英文:

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

  1. whitelist = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P']
  2. word = input('Password ')
  3. # to report on each character
  4. for ch in word.upper():
  5. if ch not in whitelist:
  6. print('Incorrect letter', ch)
  7. else:
  8. print('Correct letter', ch)
  9. # or to check the whole word
  10. for ch in word.upper():
  11. if ch not in whitelist:
  12. val = 'Incorrect letters'
  13. break
  14. else:
  15. val = 'correct letters'
  16. 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:

确定