Google Python练习'Copyspecial'默认编程出错(Error in default programming of Google's Python ex

编程入门 行业动态 更新时间:2024-10-27 02:21:41
Google Python练习'Copyspecial'默认编程出错(Error in default programming of Google's Python exercise 'Copyspecial')

在Google的Python练习copyspecial.py使用--todir选项时,我得到一个IndexError: list index out of range错误。 我该如何解决这个问题? 最令我困惑的是导致它的代码部分是由教师(来自Google / Standford)编写的部分。 我只能假定一些语法错误已经渗入到其他代码行中,或者自Python 2.7以来,内置函数语法已经发生了变化。 这个练习代码是用2.7写的。

该文件在没有使用任何选项时工作,如下所示:

打印特殊文件列表

C:\ GPE \ copyspecial \ xyz__hello __ TXT

C:\ GPE \ copyspecial \ zz__something __ JPG

DONE

这是错误:

代码:

def main(): # This basic command line argument parsing code is provided. # Add code to call your functions below. # Make a list of command line arguments, omitting the [0] element # which is the script itself. args = sys.argv[1:] if not args: print "usage: [--todir dir][--tozip zipfile] dir [dir ...]"; sys.exit(1) # todir and tozip are either set from command line # or left as the empty string. # The args array is left just containing the dirs. todir = '' if args[0] == '--todir': todir = args[1] del args[0:2] tozip = '' if args[0] == '--tozip': tozip = args[1] del args[0:2] if len(args) == 0: print "error: must specify one or more dirs" sys.exit(1) # +++your code here+++ # Call your functions

所有上述代码均直接来自google.com。 我的代码在main()被定义之前,并且在它说# +++your code here+++

我花了数小时试图解决这个问题。 我学到了很多,但不是解决方案。

我试过改变缩进。 我尝试过在' --todir '下做sys.exit(1)嵌套,但程序一直运行到' if tozip '部分,这导致我相信它是语法的。 但我找不到错放的()或: 。 我还检查了缩进。 我尝试添加一个' if args[0]: ''检查,但它不起作用,因为后来我知道,虽然是一个空列表('args[0]' = []) ,但Python不解释它作为一个实际的' False '价值。 名单继续

我非常感谢有机会让我的问题在社区中听到,而且更像是第一次张贴海报。

I get an IndexError: list index out of range error when using the --todir option in Google's Python Exercise copyspecial.py. How can I resolve this issue? What confuses me the most is that the part of code causing it is what was written by the instructor (from Google/Standford). I can only assume some syntactic error has spilled into other lines of code or that built in function syntax has changed since Python 2.7. This exercise code was written in 2.7.

The file works when no option is used, as so:

Printing list of special files

C:.\gpe\copyspecial\xyz__hello__.txt

C:.\gpe\copyspecial\zz__something__.jpg

done

This is the error:

The code:

def main(): # This basic command line argument parsing code is provided. # Add code to call your functions below. # Make a list of command line arguments, omitting the [0] element # which is the script itself. args = sys.argv[1:] if not args: print "usage: [--todir dir][--tozip zipfile] dir [dir ...]"; sys.exit(1) # todir and tozip are either set from command line # or left as the empty string. # The args array is left just containing the dirs. todir = '' if args[0] == '--todir': todir = args[1] del args[0:2] tozip = '' if args[0] == '--tozip': tozip = args[1] del args[0:2] if len(args) == 0: print "error: must specify one or more dirs" sys.exit(1) # +++your code here+++ # Call your functions

All the aforementioned code is straight from google.com. My code comes before main() is defined and after where it says # +++your code here+++

I have spent hours trying to resolve this. I've learned a lot, but not the solution.

I've tried changing indentations. I've tried doing sys.exit(1) nest under the '--todir' 'if', but the program keeps running down into the 'if tozip' part, which leads me to believe it's syntactical. But I can't find a misplaced () or :. I also checked indentations. I've tried adding an 'if args[0]:' check, but it doesn't work, because as I later learned, although an empty list ('args[0]' = []), Python does not interpret it as an actual 'False' value. The list goes on

I really appreciate the opportunity to have my question heard by the community at stackoverflow, and even more so as a first time poster.

最满意答案

据我所知,如果你做得对,你的第三次尝试应该有效:

tozip = '' if args and args[0] == '--tozip': tozip = args[1] del args[0:2]

这实际上检查列表args 。 如果它为空( [] ),则认为是False,第二个测试args[0] == '--tozip'不会被评估。

你的问题是, args本身是一个空列表, 它的计算结果为False (请参阅https://docs.python.org/2/library/stdtypes.html#truth-value-testing ),因此你不能访问args[0]并检查它是否会导致相同的Indexerror 。

但是,如果只传递其中一个没有参数的参数,你仍然会得到一个IndexError ,因为你在没有测试的情况下访问了args[1] 。

编辑(为什么代码不按原样运行?):我不认为任何python版本> = 2.4会以不同的方式解释它,但我没有证据。 这个参数传递是非常基本的。 检查格式错误的用户输入总是非常“烦人”,因为您必须处理导致大量代码的每个可能的输入。 如果你想更详细地介绍参数传递,我推荐argparse模块( argparse )。 我的感觉是,为了避免让锻炼文件的很大一部分与锻炼无关,他们只是简单地离开了它。 如果您不提供至少一个文件路径作为参数,则无论如何都会在下一步中收到错误消息:

if len(args) == 0: print "error: must specify one or more dirs" sys.exit(1)

所以代码确实按原样运行。 您只需提供正确的参数即可。

As far as I can see your third try should work if you do it right:

tozip = '' if args and args[0] == '--tozip': tozip = args[1] del args[0:2]

This actually checks the list args. If it is empty ([]), it is considered False and the second test args[0] == '--tozip' does not get evaluated.

Your problem is that args itself is an empty list which does evaluate to False (see https://docs.python.org/2/library/stdtypes.html#truth-value-testing), hence you cannot access args[0] and checking for it results in the same Indexerror.

However, you would still get an IndexError if you only pass one of the parameters without the argument because you access args[1] without testing.

EDIT (Why doesn't the code run as is?): I don't think any python versions >=2.4 would interpret this differently but I have no proof. This argument passing is very basic. Checking for malformed user input is always quite "annoying" because you have to handle every possible input which results in a lot of code. If you want to go into more detail of argument passing I recommend the argparse module (2.7, 3.5). My feeling is that to avoid having a large part of the exercise file that has nothing to do with the exercise they just left it that simple. If you don't supply at least one file path as a parameter you will get an error message in the next step anyway:

if len(args) == 0: print "error: must specify one or more dirs" sys.exit(1)

So the code does run as is. You just have to supply the right parameters.

更多推荐

本文发布于:2023-07-31 07:27:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1341902.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Copyspecial   Python   Google   Error   exercise

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!