1

示例用户输入

letters = input("Please input the scrambled letters in order: ")

现在我们都知道英文只有26个字母,而且没有一个是重复的。那么我如何确保用户输入的内容不会重复(不需要替换)?我需要使用该算法编写一个 if 语句。

if letters == nothing_duplicate:
    do something
4

2 回答 2

3
if len(letters) == len(set(letters)):
    do something
于 2013-08-15T02:04:17.220 回答
0

If you want to check for duplicates and verify that they have input every letter:

import string
if set(letters.lower()) == set(string.lowercase):
  # do something

To actually get the list of letters that are missing, you could do something like this:

>>> set(string.lowercase).difference('abcdefghijklmnopqrst')
set(['u', 'w', 'v', 'y', 'x', 'z'])
于 2013-08-15T02:39:14.420 回答