当我在 Visual Studio Code 中调试时,我的 Python 代码运行得很好,但是当我在命令提示符中打开它时,一旦按 Enter 键,它就会关闭

问题描述 投票:0回答:1

首先,我刚刚在这里注册了一个帐户,所以如果我发布了错误或其他内容,我深表歉意。但无论如何:

基本上就是标题所说的。我正在使用 Python 制作一个基于文本的假操作系统(我知道很奇怪,但请耐心等待)。当我在 Visual Studio Code 的调试器中测试它时,一切都运行良好。但是,我想在与未来用户相同的环境中对其进行测试,因此我在文件资源管理器中右键单击该文件并选择使用 Python 打开,这当然将我带到了命令提示符。

一切都很顺利,直到我按回车键,然后文本出现在屏幕上大约一纳秒,命令提示符窗口刚刚关闭。

我查了一下,很多人都说要添加文本输入,但我的已经有几个了,而且还在关闭中。再次请注意,它在 Visual Studio Code 的调试器中运行良好。

我认为我不应该发布所有 400 行代码,但我会将代码的重要部分放在下面:

import time
import getpass

#Start of Bootup:

def welcome_user():
    print("Welcome to rOS!")

def boot_up():
    time.sleep(1)
    print("Booting up...")
    time.sleep(3)
    print("The system has successfully booted.")

#End of Bootup
    
#Start of Login:

def get_password():
    password = getpass.getpass("Enter your password: ")
    return password

def create_password():
    password = getpass.getpass("Create a password: ")
    return password

def save_password(password):
    with open("password.txt", "w") as file:
        file.write(password)

def load_password():
    try:
        with open("password.txt", "r") as file:
            password = file.read().strip()
            return password
    except FileNotFoundError:
        return None
    
#End of Login

def calculator():
    #My calculator code doesn't have anything to do with the problem so I'm not posting it now.
    pass

def antivirus():
    #Again, it's not necessary for this post; it's here as a placeholder.
    pass

def desktop():
    print("------------------------------")
    print("DESKTOP")
    while True:
        program = input("Enter a program name: ")
        #The programs are simulated fyi
        if program == "list":
            print("------------------------------")
            print("Antivirus")
            print("Calculator")
            print("------------------------------")
        elif program == "antivirus":
            antivirus()
        elif program == "calculator":
            calculator()
        else:
            print("Invalid program name. Please try again.")
            continue

#More of the login stuff:

def login():
    password = load_password()
    if password:
        while True:
            entered_password = get_password()
            if entered_password == password:
                print("Login successful!")
                desktop()
                break
            else:
                print("Incorrect password. Please try again.")
    else:
        password = create_password()
        save_password(password)
        print("Password created!")

def main():
    welcome_user()
    boot_up()
    time.sleep(1)
    login()

if __name__ == "__main__":
    main()

希望代码不要太长。我的真实代码要长得多。

但无论如何,每当我在程序中创建密码后打开 py 文件并按“Enter”键时,就会出现文本,但它会立即退出命令提示符。但在 VS Code 中就可以了。

python visual-studio-code crash
1个回答
0
投票

正如我在评论中所说,如果您想运行它只是执行代码,您应该在末尾添加一个

input
,以允许停止将退出提示的执行。

import time
import getpass

#Start of Bootup:

def welcome_user():
    print("Welcome to rOS!")

def boot_up():
    time.sleep(1)
    print("Booting up...")
    time.sleep(3)
    print("The system has successfully booted.")

#End of Bootup
    
#Start of Login:

def get_password():
    password = getpass.getpass("Enter your password: ")
    return password

def create_password():
    password = getpass.getpass("Create a password: ")
    return password

def save_password(password):
    with open("password.txt", "w") as file:
        file.write(password)

def load_password():
    try:
        with open("password.txt", "r") as file:
            password = file.read().strip()
            return password
    except FileNotFoundError:
        return None
    
#End of Login

def calculator():
    #My calculator code doesn't have anything to do with the problem so I'm not posting it now.
    pass

def antivirus():
    #Again, it's not necessary for this post; it's here as a placeholder.
    pass

def desktop():
    print("------------------------------")
    print("DESKTOP")
    while True:
        program = input("Enter a program name: ")
        #The programs are simulated fyi
        if program == "exit":
            print("Goodbye!")
            break
        elif program == "list":
            print("------------------------------")
            print("Antivirus")
            print("Calculator")
            print("to exit type 'exit'")
            print("------------------------------")
        elif program == "Antivirus":
            antivirus()
        elif program == "Calculator":
            calculator()
        else:
            print("Invalid program name. Please try again.")
            continue

#More of the login stuff:

def login():
    password = load_password()
    if password:
        while True:
            entered_password = get_password()
            if entered_password == password:
                print("Login successful!")
                desktop()
                break
            else:
                print("Incorrect password. Please try again.")
    else:
        password = create_password()
        save_password(password)
        print("Password created!")

def main():
    welcome_user()
    boot_up()
    time.sleep(1)
    login()
    input("Press enter to exit the prompt.")

if __name__ == "__main__":
    main()

我还添加了

exit
程序来跳出循环并更改条件以输入防病毒和计算器语句,因为如果您将大写字母放入
list
中,您应该在条件中包含大写字母。 (我在两个函数中添加了一个
pass
,因为没有它会抛出错误)

© www.soinside.com 2019 - 2024. All rights reserved.