Python 语法错误:无效语法 end='' [英] Python SyntaxError: invalid syntax end=''

查看:120
本文介绍了Python 语法错误:无效语法 end=''的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习Head First Python"这本书,但在这段代码中遇到了问题:

data = open('sketch.txt')对于数据中的 each_line:(role, line_spoken) = each_line.split(':')打印(角色,结束='')打印('说:',结束='')打印(line_spoken,end='')数据关闭()

错误:

 文件Aula 3.py",第 12 行打印(角色,结束='')^语法错误:无效语法

草图.txt:

Man:这是争论的合适场所吗?另一个男人:我已经告诉过你一次了.男:不,你没有!另一个男人:是的,我有.男:什么时候?另一个男人:刚才.男:不,你没有!另一个男人:是的,我做到了!男:你没有!另一个男人:我告诉你,我做到了!男:你没有!另一个男人:哦,对不起,这是五分钟的争论,还是整整半小时?男:啊!(拿出钱包付钱)就五分钟.其他人:就五分钟.谢谢你.另一个男人:无论如何,我做到了.男:你肯定没有!另一个男人:现在让我们弄清楚一件事:我绝对告诉过你!男:哦,不,你没有!另一个男人:哦,是的,我做到了!男:哦,不,你没有!另一个男人:哦,是的,我做到了!男人:哦,看,这不是争论!(暂停)另一个男人:是的!男:不,不是!

我花了两天时间试图找出代码不起作用的原因.错误总是显示在end=''"中.

解决方案

您似乎在使用 Python 2.x,而不是 Python 3.x.

检查您的python版本:

<预><代码>>>>导入系统>>>系统版本'2.7.5(默认,2013 年 5 月 15 日,22:44:16)[MSC v.1500 64 位 (AMD64)]'>>>打印(1,结束='')文件<stdin>",第 1 行打印(1,结束='')^语法错误:无效语法

在 Python 3.x 中,它不应该引发语法错误:

<预><代码>>>>导入系统>>>系统版本'3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:06:53) [MSC v.1600 64 位 (AMD64)]'>>>打印(1,结束='')1>>>

I am studying the book "Head First Python" and I'm having trouble this code:

data = open('sketch.txt')

for each_line in data:
    (role, line_spoken) = each_line.split(':')
    print(role, end='')
    print(' said: ', end='')
    print(line_spoken, end='')

data.close()

Error:

  File "Aula 3.py", line 12
      print(role, end='')
               ^
SyntaxError: invalid syntax

sketch.txt:

Man: Is this the right room for an argument?
Other Man: I've told you once.
Man: No you haven't!
Other Man: Yes I have.
Man: When?
Other Man: Just now.
Man: No you didn't!
Other Man: Yes I did!
Man: You didn't!
Other Man: I'm telling you, I did!
Man: You did not!
Other Man: Oh I'm sorry, is this a five minute argument, or the full half hour?
Man: Ah! (taking out his wallet and paying) Just the five minutes.
Other Man: Just the five minutes. Thank you.
Other Man: Anyway, I did.
Man: You most certainly did not!
Other Man: Now let's get one thing quite clear: I most definitely told you!
Man: Oh no you didn't!
Other Man: Oh yes I did!
Man: Oh no you didn't!
Other Man: Oh yes I did!
Man: Oh look, this isn't an argument!
(pause)
Other Man: Yes it is!
Man: No it isn't!

I'm two days trying to figure out why the code is not working. The error is always shown in "end =''".

解决方案

It seems like you're using Python 2.x, not Python 3.x.

Check your python version:

>>> import sys
>>> sys.version
'2.7.5 (default, May 15 2013, 22:44:16) [MSC v.1500 64 bit (AMD64)]'
>>> print(1, end='')
  File "<stdin>", line 1
    print(1, end='')
                ^
SyntaxError: invalid syntax

In Python 3.x, it should not raise Syntax Error:

>>> import sys
>>> sys.version
'3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:06:53) [MSC v.1600 64 bit (AMD64)]'
>>> print(1, end='')
1>>>

这篇关于Python 语法错误:无效语法 end=''的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆