0

I made a password generator - I'm only 16 so it's probably not the best- and it outputs 8 0 and ones like 01100101 and then enderneath that it outputs the password. Well when there is a "10" in the password like FG4v10Y6 it will add another character so instead of it being FG4v10Y6 it would be FG4v10Y6M so it has nine or more characters depending on how many "10" are in it.

I'm not sure why it's doing this please help. THanx!

import pygame
import random

pygame.init()

#letters

reg = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']

CAP = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']

final_pass = []
num_let_list = []
new_list = []

i = 0

file = open("password_test","w")


def num_list_gen(num_list):
    for i in range(8):
        num_let_list.append(random.randint(0,1))
        i += 1

    for each in num_let_list:
        each = str(each)
        new_list.append(each)   
    print ''.join(new_list)

def CAP_reg_num(final_pass,num_let_list,CAP,reg):
    for each in num_let_list:
        if each == 0:
            cap_reg = random.randint(0,1)
            if cap_reg == 0:
                let1 = random.randint(0,25)
                final_pass.append(reg[let1])
            if cap_reg == 1:
                let1 = random.randint(0,25)
                final_pass.append(CAP[let1])
        if each == 1:

                    num1 = random.randint(0,10)
            num1 = str(num1)
                    final_pass.append(num1)

def main(CAP,reg,num_let_list,final_pass):
    num_list_gen(num_let_list)
    CAP_reg_num(final_pass,num_let_list,CAP,reg)
    print ''.join(final_pass)
    file.write(''.join(final_pass))
    file.close

main(CAP,reg,num_let_list,final_pass)

why did the code come out all weird on the post in some places and how do you fix it?

PygamePi
  • 11
  • 4
  • I'm not sure if I understand what you're asking. Is the issue just that your line `num1 = random.randint(0,10)` returns values from 0 to 10 (including the end points)? This is [documented](http://docs.python.org/2/library/random.html#random.randint). If you don't want the higher value included, try `random.randrange`. – Blckknght Mar 20 '13 at 04:59

1 Answers1

0

Your password generator is flipping a coin to choose between adding a letter or a number. When it chooses to add a number, you choose the number to add with:

num1 = random.randint(0,10)

However, this doesn't return a single digit number. It returns one of 11 possible values: the numbers between 0 and 10 inclusive. So one time in 11, it will add the number 10 to the string, which is, of course, two digits.

You want:

num1 = random.randint(0,9)

instead to add a single digit.

rra
  • 3,807
  • 18
  • 29
  • OH ok i see thanks! I'm glad it's something really simple, but at the same time i should have known that. – PygamePi Mar 21 '13 at 02:55