I'm writing a program which uses the diceware algorithm to generate passwords. I have come across a problem where an if statement prematurely exits two for loops without me telling it to. When I remove the if statement the loops do not exit. I have also tried to put the if statement into it's own function but this did not help.
The offending code:
def decoder(new_list, split_list):
password = ""
for string in new_list:
for pair in split_list:
if string == pair[0]:
print(string, "STRING")
print(pair, "PAIR")
password += pair[1] + " "
return password
The full program:
import random
def dice_roll(num):
rolls = [[random.randint(1, 6) for i in range(5)] for i in range(num)]
return rolls
def format_rolls(rolls):
new_list = []
for roll in rolls:
formatted_rolls = ""
for num in roll:
formatted_rolls += str(num)
new_list.append(formatted_rolls)
return new_list
def file_open():
with open("codes.txt", "r") as codes:
line_list = codes.read().splitlines()
split_list = (item.split("\t") for item in line_list)
return split_list
def decoder(new_list, split_list):
password = ""
for string in new_list:
for pair in split_list:
if string == pair[0]:
print(string, "STRING")
print(pair, "PAIR")
password += pair[1] + " "
return password
if __name__ == "__main__":
print("Your new password is:",
decoder(format_rolls(dice_roll(int(input("Number of words: ")))),
file_open()))
The file codes.txt contains a bunch of numbers and the phrases associated with them like this:
11122 able
11123 abnormal
11124 abrasion
The numbers and the phrases are separated by tab characters not spaces.
The program should output a password with as many words as defined by the user. However due to the loop exiting only one word is chosen regardless of user input.
This is the output from running this code:
Number of words: 7
11425 STRING
['11425', 'aliens'] PAIR
Your new password is: aliens
Aucun commentaire:
Enregistrer un commentaire