2016-04-06 27 views
-1

リスト(ドメイン)内の項目が文字列(電子メール)に含まれているかどうかを確認しようとしています。電子メールにはドメインがあります。 Pythonは "in 'は左のオペランドであり、リストではない文字列を必要とし、それらを一緒に比較することはできません。誰もがリスト内の単一の項目を文字列と比較する方法を知っていますか?前もって感謝します!リスト内の項目が文字列内にあるかどうかPython 2.7

at = '@' 
period = '.' 
domain = ['.com', '.edu', '.gov', '.org', '.net', '.aaa', '.aarp', 'abb', '.abbott', '.abbvie', '.abogado', '.abudhabi', '.ac', '.academy', '.accenture', '.accountant', '.accountants', '.aco', '.active', '.actor', '.ad', '.adac', '.ads', '.adult', ] 
email = raw_input("What is your email? > ") 


if period not in email: 
    print("You failed") 
else: 
    print("You have an @ symbol!") 


if period not in email: 
    print("You do not have a period in your email") 
else: 
    print("You have a period in your email") 


if domain not in email: 
    print("You do not have a domain in your e-mail. Oops!") 
else: 
    print("You have a domain in your email!") 
+0

私の方法です。または、電子メールからドメインを抽出し、ドメイン内にあるかどうかを確認することができます。 –

+1

電子メールに '.'がないと、あなたは' @ 'を持っていますか?また、なぜあなたは '電子メールにない期間:'を2回していますか?私はあなたがチュートリアルまたは2つを読む必要があると思うかもしれないと思うhttp://anandology.com/python-practice-book/ –

+0

@PadraicCunningham私は彼が 'at'の代わりに' period'を間違えたと思います。その下に 'ピリオド'チェッカーがあります。 – TheRandomGuy

答えて

2

リスト内の各項目の含有チェックを個別に行う必要があります。このかかわらずのためのいくつかの構文のPython糖があります:

(電子メールを検証する)地点まで
if any(x in email for x in domain): 
    # good: email contains one of the domains 

以上:

if all(not email.endswith(x) for x in domain): 
    # rain hellfire on user 
+1

'email.endswith(x)'はどうですか? –

+0

それはあなたと私が電子メールについて知っているものです、そうではありません。 OPのコードが彼がしたいことを示すものではありません) – schwobaseggl

+0

@schwobaseggl、OPはまた、期間がないと自動的に '@ 'を意味すると考えています –

0

あなたのロジックは、独自のコードで間違っているが、最も簡単かつ最速の方法です

:.whateverをスライスし、ドメインのセットで検索を行うには:あなたが求めを維持したい場合は

domains = {'.com', '.edu', '.gov', '.org', '.net', '.aaa', '.aarp', 'abb', '.abbott', '.abbvie', '.abogado', '.abudhabi', '.ac', '.academy', '.accenture', '.accountant', '.accountants', '.aco', '.active', '.actor', '.ad', '.adac', '.ads', '.adult',} 


email = raw_input("What is your email? > ") 

dom = email[email.rfind("."):] 

あなたはしばらく真のループを使用する必要があります

at = '@' 
period = '.' 
domains = {'.com', '.edu', '.gov', '.org', '.net', '.aaa', '.aarp', 'abb', '.abbott', '.abbvie', '.abogado', '.abudhabi', '.ac', '.academy', '.accenture', '.accountant', '.accountants', '.aco', '.active', '.actor', '.ad', '.adac', '.ads', '.adult',} 
while True: 
    email = input("What is your email? > ") 
    if at not in email: 
     print("You failed, no @ in your email addrress.") 
     continue 
    print("You have an @ symbol!") 

    if period not in email: 
     print("You do not have a period in your email") 
     continue 
    print("You have a period in your email") 

    if email[email.rfind("."):] in email: 
      print("You have a domain in your email!") 
      break   
    print("You do not have a domain in your e-mail. Oops!") 

print(email) 

あなたがendswithを使用した場合、それは単純にドメインのタプルを渡すの問題です:次に

domains = ('.com', '.edu', '.gov', '.org', '.net', '.aaa', '.aarp', 'abb', '.abbott', '.abbvie', '.abogado', '.abudhabi', '.ac', '.academy', '.accenture', '.accountant', '.accountants', '.aco', '.active', '.actor', '.ad', '.adac', '.ads', '.adult') 

sinply:

if email.endswith(domains) 
0

あなただけの電子メールアドレスかどうかを確認したかったですドメインが含まれているかどうかを判断するには、単純にpythonでfind関数を使用して、サブ文字列を取得するインデックスを返します。ここで

はあなたがdomain` `にわたってループ処理を持ち、` ​​email`は、各ドメインで終わるかどうかをチェックします

domain = ['.com', '.edu', '.gov', '.org', '.net', '.aaa', '.aarp', 'abb', '.abbott', '.abbvie', '.abogado', '.abudhabi', '.ac', '.academy', '.accenture', '.accountant', '.accountants', '.aco', '.active', '.actor', '.ad', '.adac', '.ads', '.adult', ] 


email = raw_input("What is your email? > ") 

for dom in domain: 

    if(email.find(dom) != -1): 
      print email 
関連する問題