作者yueayase (scrya)
看板Python
标题[心得] Pydev + Eclipse
时间Thu Jan 27 23:25:21 2011
如果要用Eclipse 写 Python的话, 除了下载Pydev以外,有一个小事要注意,例如:
sum = 0
while True:
a_string = input("Enter your age")
if a_string == "No"
print("exit!\n")
break
num = int(a_string)
sum = sum + num
print("current sum = %d" % sum)
这段程式码在IDLE和python command line运作得很好,但是(输入No时)在Eclipse会出现:
Traceback (most recent call last):
.....
in <module>
num = int(a_string)
ValueError: invalid literal for int() with base 10: 'No '
为什麽会这样呢?可以参考以下连结:
http://pydev.org/faq.html
原因是因为当输入No时, input解读成 No\r(可用Eclipse的Debug模式观察)
所以它认为a_string 不是 "No", 而是"No\r"
如此一来在执行 num = int(a_string) 就会出错,因为根本不能翻译成integer
但是如果改成if a_string == "No\r",
在IDLE就不正常了,因为它认为input读到的是"No"
所以要改成:
a_string = input("Enter your age").replace('\r','')
来把'\r'拿掉,
另外在Python 3.0後的版本,好像只有input(), 没有raw_input(),
两者似乎和在一起,变成只有input()了
不过Eclipse算是蛮好用的
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 218.173.157.116
1F:→ qrtt1:为什麽是 \r 你用 mac !? 01/27 23:51
2F:→ StubbornLin:用 str.strip()就可以滤掉 01/27 23:54
3F:→ yueayase:可是我是用windows 01/28 00:29
4F:→ yueayase:不知道为什麽....,但用.replace('\r','')在IDLE和eclipse 01/28 00:30
5F:→ yueayase:都正常 01/28 00:30
6F:推 cobrasgo:这个档案是在什麽状况下建立的? 01/28 00:34
7F:→ uranusjr:会不会是因为 Windows 换行是 CRLF, 而 Eclipse 的终端只 01/28 01:25
8F:→ uranusjr:弄掉 LF 所以残留 CR...? 我都是用 Eclipse 写完後切去系 01/28 01:26
9F:→ uranusjr:统终端执行所以没注意过这个问题就是了 01/28 01:26
10F:→ yueayase:直接用Eclipse的run,在console下输入资料 01/28 03:24
11F:→ yueayase:但我没设定一些external tool的东西,所以console不会像 01/28 03:26
12F:→ yueayase:shell一样 01/28 03:26
13F:→ StubbornLin:Eclipse可以设定EOL的格式 你可能选到Mac格式 01/28 13:26