如何在命令提示符Windows 10中运行python脚本

时间:2018-08-13 10:43:19

标签: python command-prompt

我正在尝试运行我的Python脚本,但该脚本最后一直自动关闭。我究竟做错了什么?我对Python很陌生,所以请不要因为缺乏知识而对我进行判断。任何建议都非常感谢

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)

代码如上...

谢谢

Omkar

3 个答案:

答案 0 :(得分:1)

您可以使用以下方法阻止控制台关闭:

Python 3:input("prompt: ")

Python 2:raw_input("prompt: ")

这些将使控制台保持活动状态,直到您按回车键(Enter)

答案 1 :(得分:0)

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)

这是示例输出:

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

答案 2 :(得分:0)

您可以将文件保存在文件系统中。例如,如果我将其另存为main.py在位置C:/Users/Me/main.py中,则可以直接转到Windows命令提示符并键入python c:/Users/Me/main.py,程序将运行。程序将随后关闭,但是您会看到输入内容,因为即使程序执行完成,命令提示符也不会退出。