[英]Invalid token from split or input method
I'm currently constructing a little python script and for some reason, I'm receiving an invalid token after I input my value. 我目前正在构建一个小的python脚本,由于某种原因,我在输入值后收到了无效的令牌。
Code: 码:
#!/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()
Error: 错误:
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
If you're on Python 2, then entering the items with input
separated by /
(ie 'MM/DD/YYYY)'
) will perform multiple integer divisions. 如果您使用的是Python 2,则输入的input
用/
分隔(即'MM/DD/YYYY)'
)将执行多个整数除法。 You will almost always get a 0
since the year will be far larger than the two others. 您几乎总是会得到0
因为年份将远远大于其他两个年份。
On the other hand, entering without the separator will raise the error you see since that input cannot be evaluated. 另一方面,如果输入不带分隔符,则会引起错误,因为该输入无法评估。
What you want is raw_input
: 您想要的是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']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.