Random Password Generator Keeps Generating the Same Password
Question: I have to write a program that generates random passwords (using ASCII values
and the chr() function) using Python and I have gotten my program to generate
a random password, but when the program loops, it keeps printing the same
random password and I have no idea what to do to fix it. If anyone could give
me some advice, that would be great because I'm new to python

here is the code that I have so far:

    
    
    import random, string
    
    LNGTH=8
    position=0
    password=""
    start=0
    stop=0
    
    while start==stop:
        input("would you like a password?")
        while position<LNGTH:
            x=random.randrange(9)
            character=chr(random.randrange(97, 97 + 26))
            choice=[str(x),character.upper(),character.lower()]
            pass_pos=random.choice(choice)
            password=password+pass_pos
            position+=1
    
        print(password)
    


Answer: You never reset `position`, so your `while` loop always returns `True` after
the first password is generated.

Because you also do not reset `password` that means you generate just the one
random password and re-display that value over and over again.

You need to reset both _inside_ the outer `while` loop:

    
    
    while start==stop:
        input("would you like a password?")
        position = 0
        password = ''
    

You could just use `while True` instead of `start == stop` there. You could
also use a `for` loop instead of your inner `while` loop, simplifying your
setup:

    
    
    password = ''
    for i in range(LNGTH):
        x=random.randrange(9)
        character=chr(random.randrange(97, 97 + 26))
        choice=[str(x),character.upper(),character.lower()]
        pass_pos=random.choice(choice)
        password=password+pass_pos
    

Cleaning that up some more by using
[`string.ascii_letters`](https://docs.python.org/3/library/string.html#string.ascii_letters)
and
[`string.digits`](https://docs.python.org/3/library/string.html#string.digits):

    
    
    import string
    import random
    
    password_characters = string.ascii_letters + string.digits
    length = 8
    
    while True:
        if input("Would you like a password? ").lower() not in {'y', 'yes'}:
            break
        password = ''.join([random.choice(password_characters)
                            for _ in range(length)])
        print(password)
    

