質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.46%
Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

解決済

2回答

1640閲覧

リストの要素を複数選択したいです。

yuutaasai

総合スコア2

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2021/06/09 07:13

前提・実現したいこと

ここに質問の内容を詳しく書いてください。

パスワードチェッカーの作成をしています。
出来はしたのですが、スマートになりませんか。
0から9まで書くのではなくまとめて記述できるようになりたいです。

発生している問題・エラーメッセージ

エラーメッセージ

該当のソースコード

python ソースコード

input1=input("input new password: ")
password=str(input1)
lis=[c for c in password]
if len(password)<8:
print("password is too short")
if "1"not in lis and "2" not in lis and "3" not in lis and "4"not in lis and "5"not in lis and "6"not in lis and "7"not in lis and "8" not in lis and "9"not in lis and "0" not in lis:
print("password should contain numbers")
if "#" not in lis and "$" not in lis and "&"not in lis and "=" not in lis:
print("password should contain # or $ or & or =")
else:
print("password is ok")

試したこと

ここに問題に対して試したことを記載してください。

補足情報(FW/ツールのバージョンなど)

ここにより詳細な情報を記載してください。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

K_3578

2021/06/09 07:17 編集

ソースコードはMarkDownのcode機能で囲んでください。 恐らく現在 ```ここに言語名を入力 python ソースコード ``` input1=input("input new password: ") 以下略 となっているものを ```Python input1=input("input new password: ") 以下略 ``` という形にしてください。
guest

回答2

0

yuutaasaiさんが求める回答ではないかもしれませんが、こういう処理をする場合には正規表現(re)を使うことをお勧めします。

こういう感じです。

python

1import re 2 3while password := input("input new password: "): 4 if len(password)<8: 5 print("password is too short") 6 elif not re.search(r'\d', password): 7 print("password should contain at least one number") 8 elif not re.search(r'[#$&=]', password): 9 print("password should contain # or $ or & or =") 10 else: 11 print("password is ok") 12 break

投稿2021/06/09 11:43

ppaul

総合スコア24666

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

yuutaasai

2021/06/10 14:51

ありがとうございます。 自分の知らない内容を教えていただき勉強になりました。
guest

0

ベストアンサー

例えば

Python

1if "1"not in lis and "2" not in lis and "3" not in lis and "4"not in lis and "5"not in lis and "6"not in lis and "7"not in lis and "8" not in lis and "9"not in lis and "0" not in lis:

の部分ですね。allを使うと綺麗になります:

Python

1if all(str(i) not in lis for i in range(10)): 2 print('password should contain numbers')

あとは

Python

1if "#" not in lis and "$" not in lis and "&"not in lis and "=" not in lis:

のところも同様に、

Python

1if all(el not in lis for el in ["#", "$", "&", "="]):

とすれば良いでしょう。

投稿2021/06/09 07:30

編集2021/06/09 07:37
kay_ventris4

総合スコア269

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

yuutaasai

2021/06/10 14:50

ありがとうございます。 勉強になりました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.46%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問