请高手帮忙看段代码
-
gw=raw_input('enter an number:')
ggw=int(gw)
if gw<20.5:
if gw==ggw:
gw=gw
else:
if gw-ggw>0.5:
gw=ggw+1
else:
gw=ggw+0.5
else:
if gw==ggw:
gw=gw
elif gw>ggw:
gw=ggw+1
print 'GROSS WEIGHT:', gw想要实现的是 如果输入的数字大于20.5 自动无条件向上舍入取整
如果输入的数字小于20.5 自动无条件取整到 .5现在每次输入整数的时候,得到的值都是+1,输入小数就报错
Traceback (most recent call last):
File "E:/Python/Study/hello.py", line 2, in <module>
ggw=int(gw)
ValueError: invalid literal for int() with base 10: '12.3' -
我来试试:
[code]
gw = raw_input('please enter number:')
try:
x, y = gw.split('.')
if int(y[0])>5 or int(x)>=21:
result = str(int(x)+1)
else:
result = x+'.5'
except:
result = gw
print 'Gross weight: %s ' % result
[/code]