-1
print "Welcome to my converter!"
#inches to centimeters
def in_inches(n):
    return(n * 2.54)


#pounds to kilograms
def in_pounds(n):
    return(n * 0.453592)

while True:
    print "Which conversion?"
    print "1 is for in to cm"
    print "2 is for lbs to kg"

    choice = raw_input("?")

    if choice == "1":
        n = raw_input("How many inches?")
        res = in_inches(n)
        print "In %s in we have %s cm." % (n, res)
    elif choice == "2":
        n = raw_input("How many pounds?")
        res = in_pounds(n)
        print str(n) + " is " + str(res) + " Kilograms"
    else:
        print "Invalid choice"

I have this code but it fails to work when I introduce it to the console. The console says:

Traceback (most recent call last):
File "converter.py", line 20, in <module>
res = in_inches(n)
File "converter.py", line 4, in in_inches
return(n * 2.54)
TypeError: can't multiply sequence by non-int of type 'float'

What does this mean and how could I fix it? Thanks in advance. `

ettanany
  • 19,038
  • 9
  • 47
  • 63
Jfelix
  • 45
  • 4
  • It means that you can't multiply a string with 2.54. You could use `float(n) * 2.54` in `in_inches` – MSeifert Dec 13 '16 at 18:19

1 Answers1

0

n is of type string, you need to convert it to integer like this:

n = int(raw_input("How many inches?"))

And,

n = int(raw_input("How many pounds?"))

raw_input() returns a string, that's why you need to use int() to convert n to an int.

ettanany
  • 19,038
  • 9
  • 47
  • 63