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

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

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

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Ubuntu

Ubuntuは、Debian GNU/Linuxを基盤としたフリーのオペレーティングシステムです。

パスワード

パスワードは主に情報にアクセスする際に扱われます。主に、アクセス可能なユーザーを限定する手段として使われます。

コードレビュー

コードレビューは、ソフトウェア開発の一工程で、 ソースコードの検査を行い、開発工程で見過ごされた誤りを検出する事で、 ソフトウェア品質を高めるためのものです。

Q&A

解決済

1回答

763閲覧

なぜかエラーがででないのに、うまくいかない

mcbkar

総合スコア7

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Ubuntu

Ubuntuは、Debian GNU/Linuxを基盤としたフリーのオペレーティングシステムです。

パスワード

パスワードは主に情報にアクセスする際に扱われます。主に、アクセス可能なユーザーを限定する手段として使われます。

コードレビュー

コードレビューは、ソフトウェア開発の一工程で、 ソースコードの検査を行い、開発工程で見過ごされた誤りを検出する事で、 ソフトウェア品質を高めるためのものです。

0グッド

0クリップ

投稿2022/11/25 04:03

編集2022/11/25 04:41

前提

他人のpythonのコードを改造してパスワードジェネレーターを作っています。
記号、数字、アルファベットそれぞれのオンオフ機能を追加したいのですが、特にエラーもでてないのにうまくいきません。

試したこと

random_char = secrets.choice(data)から
data = 〜〜でパスワードの文字を管理してると思ったので、分岐によってdataに記号や数字を渡さないプログラムを書きました。
ちゃんと分岐できたか確かめるprint文を書き、しっかり分岐していることがわかりました。
elseはなにも制限してない分岐ですが、実行の結果からelseのprintが表示されてないので、この分岐は通ってないと思われます。
それなのになぜ、結果からは制限されてないパスワードが生成されるのでしょうか?

実行の結果

-es記号、 -en数字、  -eaアルファベットをそれぞれ制限する。 $ python3 password_generator.py -es Hello, symbols Hello, symbols PASSWORD: Wx4-N2 $ python3 password_generator.py -en Hello, numbers Hello, numbers PASSWORD: k4$g$K $ python3 password_generator.py -ea Hello, alphabets Hello, alphabets PASSWORD: G03e)(

該当のソースコード

python

1# functions my want to achieve 2# 1.you can selected symbols on/off 3# 2.you can selected numbers on/off 4# 3.you can selected alphabet on/off 5# 4.auto copy , achieved in 11/24 6# 5.make a error what use -l more 74 and -ed in 11/24 7 8import argparse # to work with the arguments 9import string 10import secrets 11import pyperclip # auto copy moduel 12 13 14# function to generate the password 15def get_password(length: int) -> str: 16 # creating data 17 symbols = ['!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '-', '+'] 18 # symbols = ['!', '#', '$', '%', '&', ' (', ')','*', '+', ',', '-', '.', '/', ':', ';', '<', '=', '>', '?', '@', '[', ']', '^', '_', '`', '{', '|', '}', '~'] 19 numbers = [str(a) for a in range(10)] 20 lowercase_characters = string.ascii_lowercase 21 uppercase_characters = string.ascii_uppercase 22 23 # variable to hold the password 24 password = '' 25 26 # adding one symbol 27 password += secrets.choice(symbols) 28 29 # adding one number 30 password += secrets.choice(numbers) 31 32 # adding one lowercase character 33 password += secrets.choice(lowercase_characters) 34 35 # adding one uppercase character 36 password += secrets.choice(uppercase_characters) 37 38 # now run a for loop starting from the current length of the password 39 # all the way to the maxium length to fill in the remaining data 40 while len(password) < length: 41 # adding all the data together into a one list 42 characters = lowercase_characters + uppercase_characters 43 if args.excludesymbols: 44 data = numbers + list(characters) 45 print('Hello, symbols') 46 elif args.excludenumbers: 47 data = symbols + list(characters) 48 print('Hello, numbers') 49 elif args.excludealphabet: 50 data = symbols + numbers 51 print('Hello, alphabets') 52 else: 53 data = symbols + numbers + list(characters) 54 print('Hello.') 55 56 # getting a random character from the list 57 random_char = secrets.choice(data) 58 59 # if asked to exclude duplicates 60 if args.excludeduplicates: 61 # if character not already inside the password 62 # then add it into the password 63 if random_char not in password: 64 password += random_char 65 else: # if not asked to exclude duplicates 66 # then just add the character without checking 67 password += random_char 68 69 # create a list of the password 70 password_list = list(password) 71 # shuffle the list into random sequence 72 password = shuffle(password_list) 73 74 # returning the password 75 return password 76 77 78# shuffle function based on Fisher–Yates shuffle using secrets.choice() 79# as the integer selector 80def shuffle(password: list): 81 # n used to determine range of loop 82 n = len(password) 83 for x in range(n - 1, 0, -1): 84 # set new variable y to random int within needed index 85 y = secrets.choice(range(0, x + 1)) 86 # swap elements at index x and index y 87 password[x], password[y] = password[y], password[x] 88 # return concatenated password 89 return ''.join(password) 90 91# main method 92 93 94def main(args: argparse.Namespace) -> None: 95 # storing the length in a variable 96 length = args.length 97 98 # if the value is out of range then inform the user and exit 99 # if length < 6 or length > 20: 100 # print('ERROR: -l/--length should be in the range of 6 - 20') 101 # exit(0) 102 103 # add 104 if length > 74 and args.excludeduplicates: 105 print("ERROR: its too many length") 106 exit(0) 107 108 # this will hold the final password 109 password = get_password(length) 110 111 # printing the password to the user 112 print(f'PASSWORD: {password}') 113 pyperclip.copy(password) 114 115 116if __name__ == '__main__': 117 # creating the argparse object 118 parser = argparse.ArgumentParser() 119 120 # adding the length argument 121 parser.add_argument( 122 '-l', '--length', 123 help='Length of the password. [MIN=1], [MAX=74, if you use -l and -ed ], [DEFAULT = 6]', 124 metavar='', 125 default=6, 126 type=int 127 ) 128 129 # adding the exclude duplicates argument 130 parser.add_argument( 131 '-ed', 132 '--excludeduplicates', 133 action='store_true', 134 help='Excludes duplicate characters from the password' 135 ) 136 137 parser.add_argument( 138 '-es', 139 '--excludesymbols', 140 action='store_true', 141 help='Excludes symbols characters from the password' 142 ) 143 144 parser.add_argument( 145 '-en', 146 '--excludenumbers', 147 action='store_true', 148 help='excludes numbers characters from the password' 149 ) 150 151 parser.add_argument( 152 '-ea', 153 '--excludealphabet', 154 action='store_true', 155 help='excludes alphabet characters from the password' 156 ) 157 158 # parsing the argument 159 args = parser.parse_args() 160 161 # calling the main method with the args 162 main(args) 163

その他の情報

UbuntuとEmacsで書いてます
python は3.9.6です

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

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

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

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

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

int32_t

2022/11/25 04:21 編集

「何をしたら、どうなるはずが、どうなってしまう」のかを具体的な入力や出力を入れて書きましょう。 おっと、自分が質問をよく読んでませんでした。「実行の結果」のところでだいたい伝わりますね。失礼しました。
mcbkar

2022/11/25 04:48

確かに技術の世界では「できません」とか「うまくいかない」とか思考放棄で丸投げするのはよくないですよね;;
guest

回答1

0

ベストアンサー

python

1 password += secrets.choice(symbols) 2 password += secrets.choice(numbers) 3 password += secrets.choice(lowercase_characters) 4 password += secrets.choice(uppercase_characters)

このコードで記号1文字・数字1文字・小文字1文字・大文字1文字が必ず入るようになってます。ここでも args.exclude何とか をチェックして除外する必要があるでしょう。

投稿2022/11/25 04:28

int32_t

総合スコア20832

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

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

mcbkar

2022/11/25 04:56

このコードの必要性が全くわからなかったので、消してみたらうまく行きました!ありがとうございます! (多分小さな文字数でも必ず記号とかいれて複雑なパスワードにするコードだと思いますが、) というかコメントに一つ入れる的なことかいてありますね。ちゃんと読めば手を煩わせることもなかったのに、ごめんなさい;;
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.49%

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

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

質問する

関連した質問