0

Here is my code: I wish for it to create a password taking 3 characters from name, 3 characters from pn (Pet's name), and 3 chracters from birthday month and then adding the 1 or 2 digits from the date(day) of birth (1-31).

Currently, when I run it, it stops at the end of the user input and never generates a password.

def password():
    import random

    password = []
    name = input('Enter your name')
    pn = input('Enter your pets name')
    bday = input('Enter your birthday month')
    date = input('Enter the date you were born')
    while name != 'QUIT':
        pw = random.sample(name,3)
        password.append(pw)
        pw1 = random.sample(pn,3)
        password.append(pw1)
        pw2 = random.sample(bday,3)
    return('Your security code is', password + date)
2
  • Maybe replace while by if Commented Aug 6, 2015 at 23:45
  • name is only assigned once, before the loop. Thus if the loop starts it will never end. Commented Aug 6, 2015 at 23:55

4 Answers 4

1

Your code is entering an infinite loop. If name is not equal to 'QUIT' then the while loop condition name != 'QUIT' will remain true forever.

Sample code with some clean up:

import random

def password():
    name = input('Enter your name: ')
    if name == 'QUIT':
        # As soon as you type QUIT, you should quit
        # You shouldn't need to enter all the other information.
        return
    pn = input('Enter your pets name: ')
    bday = input('Enter your birthday month: ')
    date = input('Enter the date you were born: ')

    password_parts = []
    # You need the join because random.sample returns a list
    password_parts.append(''.join(random.sample(name,3)))
    password_parts.append(''.join(random.sample(pn,3)))
    password_parts.append(''.join(random.sample(bday,3)))
    password_parts.append(date)

    return "Your password is: " + ''.join(password_parts)

Sample program run:

Enter your name: john
Enter your pets name: tommy
Enter your birthday month: april
Enter the date you were born: 25
Your password is: nojmmtpri25
Sign up to request clarification or add additional context in comments.

Comments

0

I would suggest you to use the below modified code. Even if you don't remove while name != 'QUIT', it would still work fine :-

def password():
    import random

    password = []
    name = raw_input('Enter your name')
    pn = raw_input('Enter your pets name')
    bday = raw_input('Enter your birthday month')
    date = input('Enter the date you were born')
    while name != 'QUIT':
        pw = random.sample(name,3)
        password.extend(pw)
        pw1 = random.sample(pn,3)
        password.extend(pw1)
        pw2 = random.sample(bday,3)
        password.extend(pw2)
        password = ''.join(password)
        return('Your security code is', password + str(date))

modifications in detail :-

using raw_input instead of input removes the overhead of typing qoutes(') before entering string as name, pet name and month. Because, raw_input() takes the input in string format.

All your child lists (pw, pw1 & pw2) gives a new list. And you are appending these newly created lists to the parent list password. You have missed to append pw2. So at the end append makes the parent list password a list of 3 lists pw, pw1 & pw2.

password = [pw, pw1, pw2]    # list of lists

So, then you have to traverse each list one by one to form the password. It adds unnecessary overhead. Instead of doing that, best way to use is extend instead of append. This will append the elements of the child lists (pw, pw1 & pw2) to the parent list password. So at the end, all your generated characters are in the parent list password.

Now the line password = ''.join(password), creates a string out of all the characters in the list. No need of traversing the list. But, your code was not concatenating the three lists anywhere. So it was simply returning three lists nested in one single list (in fact two list since, you had missed out appending pw2).

Then the return statement would have failed :-

return('Your security code is', password + date)

Because, date is an integer and the code is trying to add an integer with a string, which will throw TypeError. So modify it to str(date).

This step can be omitted, if you also append this date to the parent list password. Then the join statement would have taken care of adding it to the formed password. But for that, you have to input the date in string format :-

date = raw_input('Enter the date you were born')

or append it as a string like :-

password.append(str(date))

Then your code will be as follow :- def password(): import random

    password = []
    name = raw_input('Enter your name')
    pn = raw_input('Enter your pets name')
    bday = raw_input('Enter your birthday month')
    date = raw_input('Enter the date you were born')     # Inputs the date in string format
    while name != 'QUIT':
        pw = random.sample(name,3)
        password.extend(pw)
        pw1 = random.sample(pn,3)
        password.extend(pw1)
        pw2 = random.sample(bday,3)
        password.extend(pw2)
        password.append(date)
        password = ''.join(password)
        return('Your security code is', password)

Comments

0

Your code is not stopping, it is entering an infinite loop. This is because the while loop condition will almost always be true, the only exception being if the user entered "QUIT" as their name.

You don't need a loop to generate the password, instead a simple if statement to check for a user entry of "QUIT" should do:

if name != 'QUIT':

Furthermore, presumably you want to return a string from the function so you need to convert the password list into a string:

return 'Your security code is {}{}'.format(''.join(password), date)

and, instead of using append() for the random characters (which adds a sublist to password), use extend() (which adds each item to password).

So putting that all that together:

import random

def password():
    password = []
    name = input('Enter your name')
    if name != 'QUIT':
        pn = input('Enter your pets name')
        bday = input('Enter your birthday month')
        date = input('Enter the date you were born')
        for s in name, pn, bday:
            password.extend(random.sample(s, 3))
        return '{}{}'.format(''.join(password), date)

Finally, you might want to reconsider what the function returns. By constructing and returning the password with the 'Your security code is' prefix you limit reuse of the function. If the function simply generates and returns a password it can be used in a variety of situations. And if the user quits, the function will return None. This allows the calling code to obtain the password (so that it might store it in a database for example), and to check for None which signifies that the user quit.

Comments

0

I guess that this is what actually you want to do:

def password():
    import random

    while True:
        password = []
        name = input('Enter your name')
        if name == 'QUIT':
            return
        pn = input('Enter your pets name')
        bday = input('Enter your birthday month')
        date = input('Enter the date you were born')
        pw = random.sample(name,3)
        password.append(pw)
        pw1 = random.sample(pn,3)
        password.append(pw1)
        pw2 = random.sample(bday,3)
    print 'Your security code is', password + date

It keeps asking until you type QUIT (and does not return anything)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.