首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在命令提示符下运行python脚本Windows 10

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

Stack Overflow用户
提问于 2018-08-13 18:43:19
回答 3查看 3.7K关注 0票数 1

我正在尝试运行我的Python脚本,但它总是在最后自动关闭。我做错了什么?我是Python的新手,所以请不要因为我缺乏知识而评判我。如有任何建议,我们将不胜感激

代码语言:javascript
复制
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

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-08-13 18:50:14

您可以使用以下命令停止关闭控制台:

Python 3:input("prompt: ")

Python 2:raw_input("prompt: ")

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

票数 2
EN

Stack Overflow用户

发布于 2018-08-13 19:02:36

更简单、更好、更高效的random.sample+string.printable+string.digits+string.ascii_letters

代码语言:javascript
复制
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)

下面是一个输出示例:

代码语言:javascript
复制
Do you want a weak, medium or strong password?: strong
Do you want symbols in your password? (#,@ etc.): yes
~rKc&%9Y<U31W.
票数 0
EN

Stack Overflow用户

发布于 2018-08-13 19:06:01

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51820491

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档