我应该在哪里键入第二个“打印”功能?

时间:2019-11-09 11:34:14

标签: python python-3.x

无法弄清楚为什么此代码不起作用。

for letter in name:
    print('give me', letter)
print('what did we get?', name)
SyntaxError: invalid syntax

2 个答案:

答案 0 :(得分:3)

这将导致语法错误的唯一原因是您直接在控制台中编写它,在这种情况下,只需在循环后添加一个空行:

>>> for letter in name:
...    print('give me', letter)
...
>>> print('what did we get?', name)

答案 1 :(得分:-1)

只要定义了“名称”,您的代码就应该起作用。这是我得到的输出:

name=["lisa", "maria"]
for letter in name:
    print('give me', letter)
print('what did we get?', name)

> [out] give me lisa 
> give me maria 
> what did we get? ['lisa', 'maria']

如果按Tab键并将第二个打印缩进到for循环内,则结果将如下所示:

name=["lisa", "maria"]
for letter in name:
    print('give me', letter)
    print('what did we get?', letter)

> [out] give me lisa  
> what did we get? lisa  
> give me maria  
> what did we get? maria