skip to Main Content

First of all I literally just now made an account here so I apologize if I’m posting this wrong or something. But anyway:

Basically what the title says. I’m making a text-based fake OS (weird I know, but bear with me) using Python. When I tested it in Visual Studio Code’s debugger, everything worked perfectly. However, I wanted to test it in the same environment that my future users will, so I right-clicked the file in File Explorer and selected open with Python, which of course brought me to the command prompt.

All was going well until I hit enter, then text appeared on the screen for like a nanosecond and the command prompt window just closed.

I looked it up and many people are saying to add a text input, but mine has several already and it’s still closing. And again, please note that it works fine in Visual Studio Code’s debugger.

I don’t think I should post all 400 lines of code, but I’ll put the important parts of the code below:

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()

Hope that code isn’t too long. My real code is much much longer.

But anyway, whenever I open the py file and hit "enter" after I’ve created a password in the program, text appears but it immediately exits out of command prompt. But it’s fine in VS Code.

2

Answers


  1. As I was saying in the comments if you want to run it just executing the code you should just add an input at the end that allows to stop the execution that will exit the prompt.

    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()
    

    I also added the exit program to go out of the loop and change the condition to enter the Antivirus and Calculator statements because if you put the Upper case letter in the list you should have in the condition. (I add a pass in the two function because without it is gonna throw an error)

    Login or Signup to reply.
  2. If you are executing a script in vscode, you should use the Run Python File option

    enter image description here

    If you are using the command to execute the script in a terminal, open a terminal in the directory where the script is located, and then use the python name.py command to execute the script

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search