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

时间:2016-02-19 00:47:54

标签: python

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

  

"找不到命令。"

我不知道为什么会这样,我真的可以使用一些帮助。这就是我所拥有的:

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操作系统中创建文件夹。我怎么能这样做?

1 个答案:

答案 0 :(得分:1)

在您的代码中,您可以检查很多时间,例如:

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"):