小编Phi*_*her的帖子

Python:String不会转换为float

几个小时前我写了这个程序:

while True:
    print 'What would you like me to double?'
    line = raw_input('> ')
    if line == 'done':
        break
    else:
        float(line)              #doesn't seem to work. Why?
        result = line*2
        print type(line)         #prints as string?
        print type(result)       #prints as string?
        print " Entered value times two is ", result

print 'Done! Enter to close'
Run Code Online (Sandbox Code Playgroud)

据我所知,它应该工作正常.问题是,当我输入一个值,例如6,我收到66而不是12.看起来这部分代码:

float(line)
Run Code Online (Sandbox Code Playgroud)

不起作用,并将行视为字符串而不是浮点数.我只做了一天python,所以它可能是一个新手的错误.谢谢你的帮助!

python string

2
推荐指数
2
解决办法
2324
查看次数

标签 统计

python ×1

string ×1