python练习帮助循环中的空间

时间:2013-03-27 08:27:44

标签: python

我正在学习python这里是一个示例代码

girls = ['alice','bernice','clarice']
boys = ['chris','arnold','bob']
letterGirls = {}
for girl in girls:
    letterGirls.setdefault(girl[0],[]).append(girl)
  print [b+'+'+g for b in boys for g in letterGirls[b[0]]]

我收到以下错误

 File "<stdin>", line 3
    print [b+'+'+g for b in boys for g in letterGirls[b[0]]]
                                                           ^
IndentationError: unindent does not match any outer indentation level

1)我想解决上述错误和
2)我想知道如何在python中的for循环中编写代码块   例如在上面的例子中,我检查了终端,如果我没有在第一行下面留下空格,那么语法是错误的我不清楚如何在python中为循环代码块提供空格是否有一些东西留下空格python

for girl in girls:
    letterGirls.setdefault(girl[0],[]).append(girl)
  print [b+'+'+g for b in boys for g in letterGirls[b[0]]]

更新
 基于下面的建议我更新了代码print语句在给定示例中的for循环之外,所以我在我的ubuntu终端上更新了程序

girls = ['alice','bernice','clarice']
boys = ['chris','arnold','bob']
letterGirls = {}
for girl in girls:
    letterGirls.setdefault(girl[0],[]).append(girl)
print [b+'+'+g for b in boys for g in letterGirls[b[0]]]

但是上面的代码仍然出现错误

print[b+'+'+g for b in boys for g in letterGirls[b[0]]]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 'c'

因为我在终端上编写程序,这正是我的终端的样子

@ubuntu:~$ python
Python 2.7.3 (default, Aug  1 2012, 05:14:39) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> girls = ['alice','bernice','clarice']
>>> boys = ['chris','arnold','bob']
>>> letterGirls = {}
>>> for girl in girls:
...     letterGirls.setdefault(girl[0],[]).append(girl)
... print [b+'+'+g for b in boys for g in letterGirls[b[0]]]
  File "<stdin>", line 3
    print [b+'+'+g for b in boys for g in letterGirls[b[0]]]
        ^
SyntaxError: invalid syntax
>>> print[b+'+'+g for b in boys for g in letterGirls[b[0]]]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: 'c'

3 个答案:

答案 0 :(得分:2)

同一级别的代码应该使用相同的缩进,因此'for'语句和'print'应该在一列(相同的缩进)中。建议的方法是使用4个空格从级别缩进级别。将您的代码更改为:

for girl in girls:
    letterGirls.setdefault(girl[0],[]).append(girl)
print [b+'+'+g for b in boys for g in letterGirls[b[0]]]

我的建议是在PEP8 guide中阅读更多信息,这将使您的生活更轻松

答案 1 :(得分:0)

1)“print”语句中有错误的缩进,请确保在Python中缩进4个空格以用于相同级别的块。在这种情况下,您不应在'print'之前缩进。

girls = ['alice','bernice','clarice']
boys = ['chris','arnold','bob']
letterGirls = {}
for girl in girls:
    letterGirls.setdefault(girl[0],[]).append(girl)
print [b+'+'+g for b in boys for g in letterGirls[b[0]]]

输出:

['chris+clarice', 'arnold+alice', 'bob+bernice']

2)如果要在循环中创建块语句,请确保所有语句都具有相同的缩进级别。

for girl in girls:
    letterGirls.setdefault(girl[0],[]).append(girl)
    print "This is in the loop"
print "This is not in the loop"

答案 2 :(得分:0)

如果您想要打印声明 OUTSIDE 循环:

for girl in girls:
    letterGirls.setdefault(girl[0],[]).append(girl)
print [b+'+'+g for b in boys for g in letterGirls[b[0]]]

如果您想要打印声明 INSIDE 循环:

for girl in girls:
    letterGirls.setdefault(girl[0],[]).append(girl)
    print [b+'+'+g for b in boys for g in letterGirls[b[0]]]

缩进必须对齐。 Read more here。每个缩进级别应使用4个空格。

例如,如果您想在代码中使用其他级别:

for ():
    print x
    for():
        print x

对于每个缩进级别,空格必须为4.

PS。即使您使用while循环或if / else条件等其他命令,也必须应用此功能。