我目前正在构建一个小 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/