1

I'm a new programmer and I'm trying to make a rudimentary password generator. But I keep getting this problem where my while loop never breaks.

l1 = 'q w e r t y u i o p a s d f g h j k l z x c v b n m 1 2 3 4 5 6 7 8 9 0'
l2 = l1.split()


def genpass(n):
    x = 0       if x == 0:
        password = ''
    if n < 100:
        while n > x:
            password = password + random.choice(l2)
            x + 1
        print(password)
    else:
        print 'Sorry, too long'

Can someone tell me what I'm doign wrong? Thanks.

2
  • 1
    The value of x does not change. Commented Feb 17, 2018 at 5:08
  • Assign x=x+1 in the while loop Commented Feb 17, 2018 at 5:08

6 Answers 6

2

You never change n or x here:

while n > x:
    password = password + random.choice(l2)
    x + 1

So if the condition was True initially it will always stay True and loop infinitely. Need to do x = x + 1

Incidentally this is the exact sort of bug that Pylint would catch for you.

Sign up to request clarification or add additional context in comments.

Comments

2

Please consider the following:

1) Obvious condition

    x = 0
    if x == 0:
        password = ''

You define x = 0, and then checks if x equals 0. It is invariably True. Hence, you can change it this way:

    x = 0
    password = ''

2) While loop never ends

Before you had:

while n > x:
    [some code]
    x + 1        # here was your mistake

Consider these two ways you can add 1 to the variable x:

x = x + 1

or

x += 1

Both mean the same thing.

For further enlightment: https://docs.python.org/3/reference/simple_stmts.html#augmented-assignment-statements

1 Comment

I added if x == 0 because I wasn't sure if the x = 0 in the beginning was resetting x back to 0, but thanks for fixing it.
1

Can this help? :p

import random

l1 = 'q w e r t y u i o p a s d f g h j k l z x c v b n m 1 2 3 4 5 6 7 8 9 0'
l2 = list(l1.split())


def genpass(n):
    x = 0
    password=[]
    if n < 100:
        while n > x:
            password.append(random.choice(l2))
            x+=1
        return ''.join(password)
    else:
        return('Sorry, too long')

#example with 14 char
print(genpass(14))

Comments

1
import random
l1 = 'q w e r t y u i o p a s d f g h j k l z x c v b n m 1 2 3 4 5 6 7 8 9 0'
l2 = l1.split()

def genpass(n):
  password = ''
  x = 0

  if n < 100:
    while n > x:
      password = password + random.choice(l2)
      x = x + 1
    print(password)
  else:
    print 'Sorry, too long'

genpass(10)

Comments

0

You made quite a few errors in your code. What is x+1? It will be x=x+1. Please go through the basics first. Why are you checking if x==0, right after assigning x=0? Don't you think the if will always be yes? Your code in a cleaned format. Hope this works.

import random
l1 = 'q w e r t y u i o p a s d f g h j k l z x c v b n m 1 2 3 4 5 6 7 8 9 0'
l2 = l1.split()


def genpass(n):
    x = 0
    password = ''
    if n < 100:
        while n > x:
            password = password + random.choice(l2)
            x=x + 1
        print(password)
    else:
        print ('Sorry, too long')

print("Enter how long you want your password to be")
genpass(int(input()))

Comments

0

You can try this, I've upgraded a little to generate more complex password.

import random

lower = 'q w e r t y u i o p a s d f g h j k l z x c v b n m'
nums = '1 2 3 4 5 6 7 8 9 0'.split()
upper = lower.upper().split()
spcl = '; ! # @ & $ '.split()
all = lower.split() + nums + upper + spcl

def genpass(n):
    x = 0
    if x == 0:
        password = ''
    if n < 100:
        while n > x:
            password = password + random.choice(all)
            x=x + 1
        print(password)
    else:
        print('Sorry, too long')
# generates a sample password
genpass(10)

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.