|
|
|
|
 请教找最大值问题出错在哪? - happyhappy [ 2005-08-06 11:38 | 857 byte(s)]
 Re: 请教找最大值问题出错在哪? - limodou [ 2005-08-06 14:16 | 329 byte(s)]
|
|
|
|
[Original]
[Print]
[Top]
|
inp=open('o.txt','r')
outp=open('outo.txt','w')
import math
for value in inp:
v=value.split()
f=str(v[1])
outp.write(math.ceil(float(f)))
inp.close()
outp.close()
提示错误:
Traceback (most recent call last):
File "C:/Python23/20050806.py", line 7, in -toplevel-
outp.write(math.ceil(float(f)))
TypeError: argument 1 must be string or read-only character buffer, not float
上面是说我的类型应该是字符或只读缓冲什么的,不是浮点型.我把flaot类型去掉后又提示
Traceback (most recent call last):
File "C:/Python23/20050806.py", line 7, in -toplevel-
outp.write(math.ceil(f))
TypeError: a float is required
不明白该怎么做.请教!
|
|
|
[Original]
[Print]
[Top]
|
|
[Original]
[Print]
[Top]
|
这里报的错其实是write()报的,因为你要将结果写入文件中,因此write需要一个字符串。而math.ceil返回的是一个数值不是字符串,自然报错。可以在将math.ceil转换为字符串,如:
str(math.ceil(float(f)))
BTW, float(f)是需要的,因为从文件中读出的f是一个字符串,因此需要先转为浮点数因此使用float()是必需的。
|
|
|
----
|
|
[Original]
[Print]
[Top]
|
|
|