2016-02-19 20 views
0

我正在構建希望是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

在你的代碼檢查許多時間相等這樣的:

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"): 
相關問題