问题:是否可以分配'传递'变量的关键字? 如果不可能请解释我为什么?
def validate_password(first_pwd, second_pwd):
vowels = [a,e,i,o,u]
first_length = len(first_pwd)
second_length = len(second_pwd)
passed_test = pass #<----- This bit here#
# pwd length
# is password length over 8 characters?
if first_length >= 8:
passed_test
else:
print("Password too short")
# are the passwords the same
if first_pwd == second_pwd:
passed_test
else:
print("Password Mismatch")
# are the passwords first and last character different
if first_pwd[0] != first_pwd[-1]:
passed_test
else:
print("First Charater cannot be same as the last character")
# CODE TESTING AREA #
password1 = "abcd1234"
password2 = "abcd1234"
print(validate_password(password1, password2))
^ Return False ^ invalid password
password1 = "Abcd1234"
password2 = "Abcd1234"
print(validate_password(password1, password2))
^ Return True ^ Valid Password
password1 = "Abedi23a"
password2 = "Abedi23a"
print(validate_password(password1, password2))
^ Return False ^ invalid password
# CODE TESTING AREA #
如果无法通过&#39; Pass&#39;在变量ill里面只需留下评论,告诉我它做了什么
答案 0 :(得分:2)
不,你不能 - pass
是一个陈述,而不是一个价值。好像您想将return
分配给变量。
话虽如此,为什么你甚至想要分配一个基本上意味着nothing
或ignore
的陈述?如果您不想要执行某些操作,请不要首先调用它:
def validate_password(first_pwd, second_pwd):
vowels = [a,e,i,o,u]
first_length = len(first_pwd)
second_length = len(second_pwd)
# pwd length
if first_length < 8:
print("Password too short")
elif first_pwd != second_pwd:
print("Password Mismatch")
elif first_pwd[0] == first_pwd[-1]:
print("First Charater cannot be same as the last character")
elif first_pwd[0].isalpha(vowels) != first_pwd[-1].isalpha(vowels):
# this is incomplete code there is a bunch that follow but i didnt put in here#
pass
else:
print("All is good!")
return True
return False
答案 1 :(得分:0)
pass
是Python中的保留关键字。它不能直接分配给任何变量。
为了更有意义,你可以简单地为它指定无
passed_test=None
for x in range(2):
passed_test
在元音中,文字应该用引号括起来。
str.isalpha()方法检查字符串是否仅由字母字符组成
import re
def validate_password(first_pwd, second_pwd):
first_length = len(first_pwd)
second_length = len(second_pwd)
tmp=first_pwd.lower()
# pass = pass #<----- This bit here#
# pwd length
if first_length < 8:
print("Password too short")
elif first_pwd != second_pwd:
print("Password Mismatch")
elif tmp[0].isalpha() and tmp[-1].isalpha() and \
tmp[0] == tmp[-1]:
print("First Charater cannot be same as the last character")
elif len(re.findall('[aeiou]',tmp))>2:
print("More than 2 vowels")
elif not re.search(r'[a-z]',tmp):
print("No alphabetic character")
elif first_pwd in (first_pwd.upper(),tmp):
print("All Characters are in same case")
else:
return True
return False
# CODE TESTING AREA #
password1 = "abcd123"
password2 = "abcd123"
print(validate_password(password1, password2))
password1 = "abcd1234"
password2 = "abcd1244"
print(validate_password(password1, password2))
password1 = "Abedi23a"
password2 = "Abedi23a"
print(validate_password(password1, password2))
password1 = "abed123o"
password2 = "abed123o"
print(validate_password(password1, password2))
password1 = "12345789"
password2 = "12345789"
print(validate_password(password1, password2))
password1 = "abcd1234"
password2 = "abcd1234"
print(validate_password(password1, password2))
password1 = "Abcd1234"
password2 = "Abcd1234"
print(validate_password(password1, password2))
# CODE TESTING AREA #