小编Eri*_*Lan的帖子

确保每个字母仅在密码中出现一次

刚开始学习python并尝试使用循环编写代码来检查唯一字母,如下所示:

def has_unique_letters(x):
  count = 0
  letters = set(x)
  while count < len(x):
    if x[count] in letters:
       return False
    count += 1
  return True
Run Code Online (Sandbox Code Playgroud)

但无论我在x中输入什么,它都返回false.代码有什么问题?谢谢!

python set while-loop

2
推荐指数
1
解决办法
104
查看次数

试图检查满足这些条件的字符串

尝试使用以下代码检查字符串:

def check_password(x):
  if has_unique_letters(x) == False:
    print "Warning! Please ensure letters are not repeated."
  if has_even_vowels(x) == False:
    print "Warning! Please ensure password contains an even number of vowels."
  if has_special_character(x) == False:
    print "Warning! Please ensure password contains at least one of {@, #, *, $}"
  if has_divisible_numbers(x) == False:
    print "Warning! Please ensure all numbers are divisible by 2 or 3."
  print "Sorry, your password does not meet our criteria."
print "Congratulations, your password meets our criteria." …
Run Code Online (Sandbox Code Playgroud)

python if-statement

1
推荐指数
1
解决办法
74
查看次数

标签 统计

python ×2

if-statement ×1

set ×1

while-loop ×1