python - TypeError: can't multiply sequence by non-int of type 'float' even if already parsed with float() -
# -*- coding: utf-8 -*- #1 kilogram : 2.205 pounds = x : y #from __future__ import division z=0 print "****the nicest kilos pounds converter on earth****" while z==0: select=input("1)kilos pounds\n2)pounds kilos\n") if select==1: y=float(raw_input('kilos: ')) print "is %f pounds\n" % y*2.205 z=int(raw_input('exit? [0/1] ')) elif select==2: y=float(raw_input('pounds: ')) print "is %f kilos\n" % y/2.205 z=int(raw_input('exit? [0/1] ')) print "bye bye!"
why keep getting typeerror: can't multiply sequence non-int of type 'float'? isn't y converted float after catching input? can't find what's wrong code.
you interpolating string first, multiplying result:
>>> y = 2.5 >>> "is %f pounds\n" % y*2.205 traceback (most recent call last): file "<stdin>", line 1, in <module> typeerror: can't multiply sequence non-int of type 'float'
this happens because %
operator has equal operator precedence multiplication, , operators executed left right in case.
put parenthesis around multiplication:
>>> "is %f pounds\n" % (y*2.205) 'is 5.512500 pounds\n'
do same division line:
print "is %f kilos\n" % (y/2.205)
alternatively, use str.format()
format values:
print "is {:f} pounds\n".format(y * 2.205)
and
print "is {:f} kilos\n".format(y / 2.205)
Comments
Post a Comment