2017-06-11 5 views
0

質問: 'pass'キーワードを変数に割り当てることはできますか? できない場合は、理由を教えてください。変数/名前に "pass"キーワードを割り当てることはできますか?

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 # 

Psudo Code

変数病気の内側に「合格」を置くために、そのことはできない場合だけで、それはあなたがすることはできません、

+2

クリーナーはない、というより複雑な理解/従ってくださいすることが難しいです。 – Marichyasana

+0

oh。私のコードを通過するときに私にとっては少し楽になるようです。 –

+1

実際の質問は何ですか?もちろん、あらゆる種類のデータを変数に入れて、それを渡したり処理したりすることができます。それがプログラミングの全ポイントです。 – zwer

答えて

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 # 
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 
+0

ありがとう、非常に便利です。 –

関連する問題