2016-11-11 14 views
0

私のプログラムは、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 

答えて

2
>>> from random import randint 
>>> randint(1, 3) 
2 
>>> randint(1, 3) 
3 
>>> help(randint) 
Help on method randint in module random: 

randint(a, b) method of random.Random instance 
    Return random integer in range [a, b], including both end points. 

あなたの文字列の長さが67を持っているので、それが取ることができる最大の指数は66ですが、時々、インデックス67、したがってIndexErrorを取得しようとしています。

また、最初の文字はインデックス0によって得られる:

>>> "abc"[0] 
'a' 
>>> "abc"[1] 
'b' 
>>> "abc"[2] 
'c' 
>>> "abc"[3] 
Traceback (most recent call last): 
    File "<input>", line 1, in <module> 
IndexError: string index out of range 

したがって、あなたが[randint(0, 66)]を使用する必要があります。いっそ

または:

# Declare this variable once 
possible_chars = "\"~`<>.,?/:;}{[]+_=-)(*&^%$£@!±§qwertyuiopasdfghjklzxcvbnm1234567890qwertyuiopasdfghjklzxcvbnm1234567890" 

# Use this line in both places instead of duplicating the string literal 
char = possible_chars[randint(0, len(possible_chars) - 1)] 

またはこの機能を使用し、両方の場所で:

def get_random_char(): 
    possible_chars = "\"~`<>.,?/:;}{[]+_=-)(*&^%$£@!±§qwertyuiopasdfghjklzxcvbnm1234567890qwertyuiopasdfghjklzxcvbnm1234567890" 
    return possible_chars[randint(0, len(possible_chars) - 1)] 

、最終的には:

from random import choice 

def get_random_char(): 
    possible_chars = "\"~`<>.,?/:;}{[]+_=-)(*&^%$£@!±§qwertyuiopasdfghjklzxcvbnm1234567890qwertyuiopasdfghjklzxcvbnm1234567890" 
    return choice(possible_chars) 
+0

すなわちcharsの値外str()を置くOP *は代わりに、設定された整数の動的な値を使用する必要がありますが、あなたは間違っていませんよ。 –

+0

@ SterlingArcher done。 –

0

は、あなたのコード内の2つのことをそこご覧ください。

  1. 明示的にchars = str()にこの変数を2回宣言する必要はありません。 25行目で
  2. は:

chars += str("\"~<>.,?/:;}{[]+_=-)(*&^%$ £@!+§qwertyuiopasdfghjklzxcvbnm1234567890" [randint(1, 67)])

関連する問題