将输入字符串与某些选项进行比较时出现意外输出

2024-09-29 20:18:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在构建一个有望成为MS-DOS风格的操作系统,但是我在文件夹方面遇到了困难。每当我尝试创建或更改目录时,都会收到一条消息

"Command not found."

我不知道为什么会这样,我真的需要一些帮助。我得到的是:

import os

def cmdLvl2():
    print("Use the 'leave' command to shut down the system. Use the 'type' command to start a text editor. You can also type 'clear' to clear the screen. Type 'help -a' for more options.")
    tcmdLvl2 = input("~$: ")
    if tcmdLvl2 == ("leave"):
        quit()
    if tcmdLvl2 == ("type"):
        typer()
    if tcmdLvl2 == ("clear"):
        os.system('cls')
    if tcmdLvl2 == (""):
        dcmdLvl2()
    if tcmdLvl2 == ("help -a"):
        print("You can use the command 'md' to make a new directory. Use the cd command to access this directory. Additionally, use 'list' to show all sub-folders in your current directory.")
        cmdLvl2()
    if tcmdLvl2 == ("md"):
        directoryName = input("Name of directory: ")
        os.mkdir(directoryName)
        if not os.path.exists(directoryName):
            os.mkdir(directoryName)
            dcmdLvl2()
    if tcmdLvl2 == ("cd"):
        changedDIR = input("Directory name: ")
        os.chdir(changedDIR)
        if not os.path.exists(changedDIR):
            print("Directory not found.")
            dcmdLvl2()
    if tcmdLvl2 == "list":
        os.system('dir')
    if tcmdLvl2 != ("leave", "type", "clear", "", "help -a", "cr", "cd", "list"):
        print("Command not found.")
    dcmdLvl2()

def dcmdLvl2():
    tcmdLvl2 = input("~$: ")
    if tcmdLvl2 == ("leave"):
        quit()
    if tcmdLvl2 == ("type"):
        typer()
    if tcmdLvl2 == ("clear"):
        os.system('cls')
    if tcmdLvl2 == (""):
        dcmdLvl2()
    if tcmdLvl2 == ("help"):
        cmdLvl2()
    if tcmdLvl2 == ("cr"):
        directoryName = input("Name of directory: ")
        os.mkdir(directoryName)
        if not os.path.exists(directoryName):
            os.mkdir(directoryName)
            dcmdLvl2()
    if tcmdLvl2 == ("cd"):
        changedDIR = input("Directory name: ")
        os.chdir(changedDIR)
        if not os.path.exists(changedDIR):
            print("Directory not found.")
            dcmdLvl2()
    if tcmdLvl2 == ("list"):
        os.system('dir')
    if tcmdLvl2 != ("leave", "type", "clear", "", "help", "help -a", "cr", "cd", "list"):
        print("Command not found.")
    dcmdLvl2()

def typer():
    print("Start typing to get started. Unfortunately, you cannot currently save your files.")
    typerCMD = input("  ")
    dcmdLvl2()

def CMDLine():
    print("Hello, and welcome to your new operating system. Type 'help' to get started.")
    cmd = input("~$: ")
    if cmd == ("help"):
        cmdLvl2()
    if cmd == ("leave"):
        quit()
    if cmd == ("type"):
        typer()
    if cmd == ("clear"):
        os.system('cls')
    if cmd == (""):
        dcmdLvl2()
    if cmd == ("help -a"):
        print("You can use the command 'cr' to make a new directory. Use the sd command to access this directory. Additionally, use 'list' to show all sub-folders in your current directory.")
        cmdLvl2()
    if cmd != ("leave", "type", "clear", "", "help -a"):
        print("Command not found.")
    dcmdLvl2()

def redirect():
    signIn()

def mUserRedirect():
    makeUser()

def PwordSignIn():
    rPword = input("Password: ")
    with open('passwords.txt', 'r') as f:
        for line in f:
            if rPword == (line):
                CMDLine()
            else:
                print("Incorrect password.")
                signIn()

def signIn():
    rUname = input("Username: ")
    with open('usernames.txt', 'r') as f:
        for line in f:
            if rUname == (line):
                PwordSignIn()
            else:
                print("Username not found.")
                mUserRedirect()

def makeUser():
    nUname = input("New username: ")
    nPword = input("Create a password for the user: ")

    with open('usernames.txt', 'w') as f:
        f.write(nUname)
    with open('passwords.txt', 'w') as f:
        f.write(nPword)
    signIn()

print("Create a new user? Warning: This will delete any other users. (Y/N) ")
nUser = input("")
if nUser == ("N"):
    signIn()
if nUser == ("n"):
    signIn()
if nUser == ("Y"):
    makeUser()
if nUser == ("y"):
    makeUser()

另外,如果有人能帮我设置文件夹,我希望它只在程序中创建文件夹,而不是在我的基本Windows操作系统中创建文件夹。我怎么能这么做


Tags: thetoinputifosdeftypenot
1条回答
网友
1楼 · 发布于 2024-09-29 20:18:05

在您的代码中,您多次检查是否存在如下等式:

tcmdLvl2 == ('somestring')

或者对于这样的不平等:

tcmdLvl2 != ("leave", "type", "clear", "", "help", "help -a", "cr", "cd", "list"):

首先检查字符串是否相等,在本例中,它将起作用,因为在本例中括号是多余的。但是,在第二种情况下,项(字符串)之间用逗号分隔,对于python来说,逗号是元组的定义

与使用==!=的元组相比,这并不能满足您的需要。 您的意图似乎是检查命令是否是这些字符串中的任何一个。在这种情况下,使用in检查命令是否是使用圆括号生成的tuple中的任何单词

所以把支票改成:

tcmdLvl2 not in ("leave", "type", "clear", "", "help", "help -a", "cr", "cd", "list"):

相关问题 更多 >

    热门问题