奇数Python 3错误

奇数Python 3错误,python,debugging,python-3.x,Python,Debugging,Python 3.x,因此,我正在用Python编写一个shell,文件名是jsh.py。当我运行代码时,由于某种原因它不起作用。弹出一个窗口,但立即关闭。我的代码出了什么问题 import os import random import time import path import string import sys commandAvail = 'ls where chdir mk mkdir cp mv rm rmdir view ext shutdown edit unix dos man-all man

因此,我正在用Python编写一个shell,文件名是
jsh.py
。当我运行代码时,由于某种原因它不起作用。弹出一个窗口,但立即关闭。我的代码出了什么问题

import os
import random
import time
import path
import string
import sys

commandAvail = 'ls where chdir mk mkdir cp mv rm rmdir view ext shutdown edit unix dos man-all man-unix man-dos'
commandAvailUnix = 'exec permission group'
commandAvailDos = ' '
print('Welcome to jsh alpha 1!')
commandPlatform = input('System: (UNIX/DOS):')
commandLine()

def commandLine():
        while 1 == 1:
                command = input('$ ')
                if command in commandAvail:
                        if command.startswith('ls'):
                                commandKeyword = 'ls'
                                commandOptions = command - commandKeyword
                                ls(commandOptions)
                        elif command.startswith('where'):
                                commandKeyword = 'where'
                                commandOptions = command - commandKeyword
                                where()
                        elif command.startswith('chdir'):
                                commandKeyword = 'chdir'
                                commandOptions = command - commandKeyword
                                chdir()
                        elif command.startswith('mk'):
                                commandKeyword = 'mk'
                                commandOptions = command - commandKeyword
                                mk()
                        elif command.startswith('mkdir'):
                                commandKeyword = 'mkdir'
                                commandOptions = command - commandKeyword
                                mkdir()
                        elif command.startswith('cp'):
                                commandKeyword = 'cp'
                                commandOptions = command - commandKeyword
                                cp()
                        elif command.startswith('mv'):
                                commandKeyword = 'mv'
                                commandOptions = command - commandKeyword
                                mv()
                        elif command.startswith('rm'):
                                commandKeyword = 'rm'
                                commandOptions = command - commandKeyword
                                rm()
                        elif command.startswith('rmdir'):
                                commandKeyword = 'rmdir'
                                commandOptions = command - commandKeyword
                                rm()
                        elif command.startswith('view'):
                                commandKeyword = 'view'
                                commandOptions = command - commandKeyword
                                rm()
                        elif command.startswith('edit'):
                                commandKeyword = 'edit'
                                commandOptions = command - commandKeyword
                                edit()
                        elif command == 'man-all':
                                print('Commands that work for all underlying platforms:')
                                print(commandAvail)
                        elif command == 'man-unix':
                                print('Commands that only work on Unix systems:')
                                print(commandAvailUnix)
                        elif command == 'man-dos'
                                print('Commands that only work on DOS systems:')
                                print(commandAvailDos)
                        elif command.startswith('unix'):
                                commandType = 'unix'
                                unix()
                        elif command.startswith('dos'):
                                commandType = 'dos'
                                dos()
                        elif command.startswith('ext'):
                                commandKeyword = 'ext'
                                commandOptions = command - commandKeyword
                                ext()
                        elif command == 'shutdown':
                                sys.quit(0)
                        else:
                                print('jsh has experienced an internal error and has to shutdown.')
                                sys.quit(10)
                else:
                        print('Command \'' + command + '\' not recognized as internal or external.')

def ls(options):
        if commandPlatform == 'UNIX':
                os.system('ls' + options)
        else:
                os.system('dir' + options)
        commandLine()

您忘了在elif命令=='man-dos'之后加上“:”


只需运行:python3您的脚本即可进行调试。

当前代码中有几个错误:

  • 首先,您有一些混乱的缩进,包括用于缩进的制表符和空格的混合。这在Python中非常糟糕,因为不正确的缩进是一种语法错误。我已经修复了问题中的代码以正确显示(每个选项卡使用8个空格),但您也需要在文件中修复它。另外,请注意Python约定是每个块缩进四个空格,因此您可能希望在默认情况下将编辑器设置为缩进四个空格

  • 其次,顶层检查的逻辑不正确。您正在测试整个命令字符串是否包含在
    commandAvail
    变量中的命令字符串中。如果命令除了命令名之外还有参数,那么这样做是错误的。我建议您
    split
    命令字符串以获得术语列表,然后仅根据可用命令测试第一个术语。我建议将
    commandAvail
    拆分为一组字符串,而不是进行子字符串搜索:

    # make a set of commands that we recognize
    commandAvail = set('ls where chdir mk mkdir cp'.split()) # trimmed for space
    
    # then later, test against it
    command = input().split()
    if command[0] in commandAvail: # this is much more efficient!
        # do stuff
    
  • 最后,您试图从命令字符串中删除命令本身的方式是错误的。您不能从另一个字符串中减去一个字符串(如果您尝试,将得到
    TypeError
    )。幸运的是,我为上一个问题建议的解决方案在这里会有所帮助。由于
    split
    调用,您将获得一个术语列表,而不是进行字符串操作。现在,您只需对第一项(命令)进行测试,并将其余的作为参数传递

    事实上,这意味着一个比当前拥有的
    if
    /
    elif
    块更简单的实现。使用字典在命令字符串和实现它们的函数之间映射:

    # map command names to the functions that implement them
    command_dict = {"ls":ls, "where":where, "chdir":chdir} # snipped
    
    # the main loop
    while True:
        # command is first term, the rest go into args
        command, *args = input().split()
    
        try: # use "easier to ask forgiveness than permission" idiom
            command_dict[command](*args) # call function from command_dict
        except KeyError:
            print("Unknown command: {}".format(command))
    

  • 您的代码正是这样缩进的吗?请尝试从控制台运行脚本,而不是直接运行脚本。你应该得到一个有更多信息的回溯。