2013-03-09 117 views
1

我很确定所有的代码都是正确的;我相信这个错误可能是安装文件的错误。奇怪的Python 3错误

a,b = input('Enter in format number^power: ').split('^') 
a = int (a) 
b = int (b) 
result = a**b 
print (result) 
input() 

当我在IDLE中运行它时,它完美地工作;但是,如果我在终端中运行脚本,它会给我这个错误:

Traceback (most recent call last): 
File "C:\Users\xxx\Desktop\calculator.py", line 1, in <module> 
a,b = input('Enter a range: ').split('^') 
AttributeError: 'int' object has no attribute 'split' 

我该做什么错?

+0

您正在Python 2中运行此代替。 – 2013-03-09 23:01:34

回答

3

您与蟒蛇2.

运行它在Python 2,input返回之前评估输入,所以如果你做提示说,它会返回一个int。

0

如果它在IDLE中正常工作,但在终端中失败,那么很可能python文件的标准处理程序未设置为Python 3,而是Python 2.如果您直接调用脚本,即仅使用./scriptname.py,则shebang将决定使用哪个Python分析器来执行脚本。

使用Python 3,添加以下家当线在你的文件的开始:

#!/usr/bin/env python3 

注意,这也支持在Windows按PEP-397