私のプログラムは、char変数に以下の文字を使用して5文字のランダムなパスワードを生成します。問題は私が信じているランディントにあり、理由を理解できません。Python - IndexError:文字列インデックスが範囲外です
from random import randint
print("1. 5 Letters")
enc = input("Please enter your desired option: ")
if enc == "1":
chars = str()
for i in range (1, 6+1):
chars += str("\"~`<>.,?/:;}{[]+_=-)(*&^%$£@!±§qwertyuiopasdfghjklzxcvbnm1234567890qwertyuiopasdfghjklzxcvbnm1234567890" [randint(1, 67)])
print("Your password is: " + chars)
print("\n")
yon = input("Do you want a new password (yes or no): ")
if yon == "yes":
np = int(input("How many new passwords do you want: "))
print("\n")
count = 0
for i in range(1, np+1):
count += 1
chars = str()
for i in range (1, 6 + 1):
chars += "\"~`<>.,?/:;}{[]+_=-)(*&^%$£@!±§qwertyuiopasdfghjklzxcvbnm1234567890" [randint(1, 67)]
print("Your password is : " + str(chars) + " This is password number: " + str(count) + "/" + str(np))
print("\n")
elif yon == "no":
print("Goodbye.")
私のプログラムが追加のパスワードを生成する部分に到達した後、このエラーが発生します。
Traceback (most recent call last):
File "/Users/rogerhylton/Desktop/Coding/Python/te.py", line 25, in <module>
chars += "\"~`<>.,?/:;}{[]+_=-)(*&^%$£@!±§qwertyuiopasdfghjklzxcvbnm1234567890" [randint(1, 67)]
IndexError: string index out of range
すなわち
chars
の値外str()
を置くOP *は代わりに、設定された整数の動的な値を使用する必要がありますが、あなたは間違っていませんよ。 –@ SterlingArcher done。 –