python - 来自 split 或输入法的无效标记

标签 python

我目前正在构建一个小 python 脚本,由于某种原因,我在输入值后收到无效 token 。

代码:

#!/usr/bin/python

def main():
    frankOceanRelease()

def frankOceanRelease():
    info = str(input('Enter the date in which Frank will drop his album (MM/DD/YYYY):')).split("/")
    if info[1] == "13" and info[0] == "11":
        return('Let\'s hope he doesn\'t flake')
    elif info[2] == "3005":
        return('This might happen, but no guarantee')
    else:
        return('Nah man, this album ain\'t out yet!')

if __name__ == "__main__":
    main()

错误:

File "./frankOceanRelease.py", line 16, in <module>
    main()
File "./frankOceanRelease.py", line 4, in main
    frankOceanRelease()
File "./frankOceanRelease.py", line 7, in frankOceanRelease
    info = input('Enter the date in which Frank will drop his album (MM DD YYYY):').split()
File "<string>", line 1
    08 07 2016
     ^
SyntaxError: invalid token

最佳答案

如果您使用的是 Python 2,则使用 input 输入项目,并用 / 分隔(即 'MM/DD/YYYY)') 将执行多个整数除法。您几乎总是会得到 0,因为年份将远远大于其他两个年份。

另一方面,不带分隔符的输入会引发您看到的错误,因为无法评估该输入。

你想要的是raw_input:

>>> info = raw_input('Enter the date in which Frank will drop his album (MM/DD/YYYY):').split("/")
Enter the date in which Frank will drop his album (MM/DD/YYYY):11/06/2016
>>> info
['11', '06', '2016']

关于python - 来自 split 或输入法的无效标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38817377/

相关文章:

python - 通过 python 脚本获取特定系统的详细信息时出错

python - Django form : fields defined by query result, 单 View 同时更新多个对象

python - syncdb 和 migrate 有什么区别?

python - 找到原始负载,如何访问?

python - REST服务中的错误方案

python - 部署无服务器 Django 应用程序?

python - 使用子进程调用的程序 - 不打印记录器消息?

python - PyDev Eclipse 中的 "source folder"和 "pydev package"有什么区别?

python - RegEx 如何根据特定位置的字符否定匹配

python - 如何在 mac 上结束 Tkinter python 模块,这样我就不必在程序完成后强制退出 python 启动器?