1

I am trying to run my Python script but it keeps on closing automatically at the end. What am i doing wrong? I am pretty new to Python so please don't judge me for my lack of knowledge. Any suggestions are much appreciated

import random

characters = ["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","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",'1','2','3','4','5','6','7','8','9','0',"!","@","#","$","%","&","*","(",")"]

characterswosymbols = ["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",'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']

strength = input("Do you want a weak, medium or strong password?: ").lower()

new_password = []

def password(strength):
    if strength == 'weak':
        symbols = input("Do you want symbols in your password? (#,@ etc.): ").lower()
        if symbols == 'yes':
            while len(new_password) != 8:
                new_password.append(characters[random.randint(1, 70)])
        elif symbols == 'no':
            while len(new_password) != 8:
                new_password.append(characterswosymbols[random.randint(1, 70)])

    elif strength == 'medium':
        symbols = input("Do you want symbols in your password? (#,@ etc.): ").lower()
        if symbols == 'yes':
            while len(new_password) != 11:
                new_password.append(characters[random.randint(1, 70)])
        elif symbols == 'no':
            while len(new_password) != 11:
                new_password.append(characterswosymbols[random.randint(1, 70)])

    elif strength == 'strong':
        symbols = input("Do you want symbols in your password? (#,@ etc.): ").lower()
        if symbols == 'yes':
            while len(new_password) != 14:
                new_password.append(characters[random.randint(1, 70)])
        elif symbols == 'no':
            while len(new_password) != 14:
                new_password.append(characterswosymbols[random.randint(1, 70)])


    return new_password

password(strength)

new_password = "".join(new_password)

print(new_password)

The code is as above...

Thanks,

Omkar

7
  • just use python script.py Commented Aug 13, 2018 at 10:46
  • I can get it to run, but after the script finishes, it automatically closes the window. How can I get it to stop closing automatically? Commented Aug 13, 2018 at 10:47
  • 1
    Do you mean the command prompt is closing? I can run the code fine on my machine Commented Aug 13, 2018 at 10:49
  • Yeah. That's what I meant. It displays the password for a fraction of a second then closes. Commented Aug 13, 2018 at 10:51
  • 1
    @ReeceMercer and string.printable for all characters Commented Aug 13, 2018 at 11:05

3 Answers 3

2

You can stop the console from closing using:

Python 3: input("prompt: ")

Python 2: raw_input("prompt: ")

These will keep the console alive until you press Return (Enter)

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

3 Comments

Thanks for the advice.
Sorry. How do you do that?
There should be a hook on the left side of my answer, click that! :)
0

Even easier, better, and more efficient, random.sample+string.printable+string.digits+string.ascii_letters:

import random,string

strength = input("Do you want a weak, medium or strong password?: ").lower()


def password(strength):
    new_password = []
    if strength == 'weak':
        symbols = input("Do you want symbols in your password? (#,@ etc.): ").lower()
        if symbols == 'yes':
            new_password.extend(random.sample(string.printable.rstrip(),8))
        elif symbols == 'no':
            new_password.extend(random.sample(string.digits+string.ascii_letters,8))

    new_password = []
    if strength == 'medium':
        symbols = input("Do you want symbols in your password? (#,@ etc.): ").lower()
        if symbols == 'yes':
            new_password.extend(random.sample(string.printable.rstrip(),11))
        elif symbols == 'no':
            new_password.extend(random.sample(string.digits+string.ascii_letters,11))

    new_password = []
    if strength == 'strong':
        symbols = input("Do you want symbols in your password? (#,@ etc.): ").lower()
        if symbols == 'yes':
            new_password.extend(random.sample(string.printable.rstrip(),14))
        elif symbols == 'no':
            new_password.extend(random.sample(string.digits+string.ascii_letters,14))


    return new_password

new_password = "".join(password(strength))

print(new_password)

Here's an example output:

Do you want a weak, medium or strong password?: strong
Do you want symbols in your password? (#,@ etc.): yes
~rKc&%9Y<U31W.

Comments

0

You can save the file on your filesystem. If I for example would save this as main.py in the location C:/Users/Me/main.py I could simply go to the Windows command prompt and type python c:/Users/Me/main.py and the program would be run. The program will close afterwards, but you will get to see the input because the command prompt wont exit even when the program execution finishes.

Comments