热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

SyntaxError:codecademy程序中的语法无效-SyntaxError:Invalidsyntaxinacodecademyprogram

inthePygLatin1011exerciseontheCodecademyPythoncourse,ihavetodothefollowing:在关于Codec

in the PygLatin 10/11 exercise on the Codecademy Python course, i have to do the following:

在关于Codecademy Python课程的PygLatin 10/11练习中,我必须做以下事情:

Set new_word equal to the slice from the 1st index all the way to the end of new_word. Use [1:len(new_word)] to do this.

将new_word设置为等于从第一个索引一直到new_word结尾的切片。使用[1:len(new_word)]执行此操作。

And I receive the following error:

我收到以下错误:

File "python", line 9
    new_word = [1:len(new_word)]
                 ^
SyntaxError: invalid syntax

And this is the code:

这是代码:

File "python", line 9
    new_word = [1:len(new_word)]
                 ^
SyntaxError: invalid syntax

Here you will find screenshots which might be of help:

在这里,您将找到可能有帮助的屏幕截图:

enter image description here

This might be a very stupid question, but I really want to understand where I am wrong with this, soIi don't make the same mistake in the future. Thanks for the help in advance.

这可能是一个非常愚蠢的问题,但我真的想知道我错在哪里,所以我不会在将来犯同样的错误。我在这里先向您的帮助表示感谢。

4 个解决方案

#1


This is how the code should be:

这是代码应该是这样的:

pyg = 'ay'

original = raw_input('Enter a word:')

if len(original) > 0 and original.isalpha():
    word = original.lower()
    first = word[0]
    new_word = word + first +pyg
    new_word = new_word[1:]
    print new_word
else:
    print 'empty'

Let's say the input is Hello. This line: new_word = word + first +pyg will give you hellohay but what you need is ellohay so you need to slice the new_wordin order to lose the first h which is new_word[0] so, new_word should start with e, which is new_word[1] this is why you need this line: new_word = new_word[1:]

假设输入为Hello。这一行:new_word = word + first + pyg会给你hellohay但你需要的是ellohay所以你需要切片new_wordin命令以丢失第一个h,即new_word [0]所以,new_word应该以e开头,即new_word [1]这就是你需要这一行的原因:new_word = new_word [1:]

The output is:

输出是:

ellohay

#2


This is wrong: new_word = [1:len(new_word)]

这是错误的:new_word = [1:len(new_word)]

This is right: new_word = new_word[1:len(new_word)]

这是对的:new_word = new_word [1:len(new_word)]

Another thing, if you want to slice starting from i to the end of your list, you can do:

另外,如果你想从i切换到列表的末尾,你可以这样做:

new_word = new_word[1:]

#3


You use square brackets for slicing, but you have to specify the thing you are slicing.

您可以使用方括号进行切片,但必须指定要切片的内容。

Just like you can access specific elements of lists, strings and the like, you can also access sublists and substrings. Like so:

就像您可以访问列表,字符串等特定元素一样,您也可以访问子列表和子字符串。像这样:

a = "this is a long string"
a[0] == "t"
a[0:4] == "this"
a[1:len(a)] == "his is a long string"

You're getting the SyntaxError because you didn't specify the thing you are slicing.

您正在获取SyntaxError,因为您没有指定要切片的内容。

#4


You need to take a slice of new_word, not a slice of nothing.

你需要拿一片new_word,而不是一片什么。


推荐阅读
author-avatar
尚福惠珠綺裕
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有