相关疑难解决方法(0)

使用八进制数时无效的令牌

我是python的初学者,我正在尝试在我的脚本中使用八进制数字,但是当我尝试它时,它会返回我的错误:

>>> a = 010
SyntaxError: invalid token (<pyshell#0>, line 1)
>>> 01
SyntaxError: invalid token (<pyshell#1>, line 1)
Run Code Online (Sandbox Code Playgroud)

我的代码有问题吗?我正在使用Python3(并阅读python 2.2书)

python syntax octal python-3.x

53
推荐指数
1
解决办法
2万
查看次数

为什么这个字典定义会引发语法错误?

可能重复:
lVals = [1,08,2011]有什么理由抛出异常吗?

我正在定义一个字典来将日期数字映射到它们各自的单词.由于某种原因,以下代码引发"SyntaxError:invalid token"并突出显示"08"

days = {01:"first", 02:"second", 03:"third", 04:"fourth", 05:"fifth", 06:"sixth", 07:"seventh", 08:"eighth", 09:"nineth", 10:"tenth", 
    11:"eleventh", 12:"twelvth", 13:"thirteenth", 14:"fourteenth", 15:"fifteenth", 16:"sixteenth", 17:"seventeenth", 18:"eighteenth",
    19:"nineteenth", 20:"twentieth", 21:"twenty-first", 22:"twenty-second", 23:"twenty-third", 24:"twenty-fourth", 25:"twenty-fifth",
    26:"twenty-sixth", 27:"twenty-seventh", 28:"twenty-eighth", 29:"twenty-nineth", 30:"thirtieth", 31:"thirty-first"}
Run Code Online (Sandbox Code Playgroud)

修改代码,使08和09成为98和99停止任何错误,如下面的代码:

days = {01:"first", 02:"second", 03:"third", 04:"fourth", 05:"fifth", 06:"sixth", 07:"seventh", 98:"eighth", 99:"nineth", 10:"tenth", 
    11:"eleventh", 12:"twelvth", 13:"thirteenth", 14:"fourteenth", 15:"fifteenth", 16:"sixteenth", 17:"seventeenth", 18:"eighteenth",
    19:"nineteenth", 20:"twentieth", 21:"twenty-first", 22:"twenty-second", 23:"twenty-third", 24:"twenty-fourth", 25:"twenty-fifth",
    26:"twenty-sixth", 27:"twenty-seventh", 28:"twenty-eighth", 29:"twenty-nineth", 30:"thirtieth", 31:"thirty-first"}
Run Code Online (Sandbox Code Playgroud)

并且输出变为:

{1: 'first', 2: 'second', 3: 'third', …
Run Code Online (Sandbox Code Playgroud)

python syntax dictionary

1
推荐指数
1
解决办法
1085
查看次数

标签 统计

python ×2

syntax ×2

dictionary ×1

octal ×1

python-3.x ×1