split或input方法中的令牌无效

2024-10-01 00:29:45 发布

您现在位置:Python中文网/ 问答频道 /正文

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

代码:

#!/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()

错误:

^{pr2}$

Tags: the代码info脚本inputalbumreturnif
1条回答
网友
1楼 · 发布于 2024-10-01 00:29:45

如果您在Python 2上,那么输入用^{{cd2>}(即^{{cd3>})分隔的项目将执行多个整数分区。你几乎总是会得到一个^{cd4>}因为这一年将远远大于另外两个。

另一方面,如果不使用分隔符输入,将引发您看到的错误,因为无法评估该输入。

您想要的是^{cd5>}:

>>> 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']

相关问题 更多 >