无法弄清楚为什么此代码不起作用。
for letter in name:
print('give me', letter)
print('what did we get?', name)
SyntaxError: invalid syntax
答案 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