我的程序旨在使用char變量中下面給出的字符生成一個長度爲5個字符的隨機密碼。問題在於我相信的randint,並且不知道爲什麼。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
OP *應*使用動態值,而不是一組整數,但你沒看錯。 –
@SterlingArcher完成。 –