Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

How would I create a simple length converter in Python?

Ok, well I'm a beginner in python, a beginner at the VERY beginning :P (I'm only on the 11th newboston tutorial :P) Well, maybe I'm getting ahead of myself a bit, but I wanted to create a length converter, like something that will convert feet to inches and centimeters. I wrote this, and it worked:

print "Unit conversion"

X = int(raw_input("How many feet are there?"))

ap = int(X)

aq = int(X*12)

ar = int(X*30.48)

total1 = ap

total2 = aq

total3 = ar

print "The totals are: " + `total1` + " feet or " + `total2`+ " inches or " + `total3` + " centimeters"

raw_input ("Press <enter>")

But now I want to write something that only converts to what is input at the very beginning, and so I wrote this, and it's probably really wrong and random, but here it is:

print "Unit conversion"

convert = str(raw_input("What would you like to convert the number of feet to?"))

amount = int(raw_input("How many feet are there?"))

A = int(amount*12)

B = int(amount*30.48)

if convert == str(inches):

print "The total is" + `A` + "inches"

if convert == str(centimeters):

print "The total is" + `B` + "centimeters"

raw_input ("Press <enter>")

Sooo... Can you tell me what all is wrong with ^ and how to fix it? Thank you!!!!

Update:

And I also tried this:

print "Unit conversion"

convert = str(raw_input("What would you like to convert the number of feet to?"))

amount = int(raw_input("How many feet are there?"))

A = int(amount*12)

B = int(amount*30.48)

if convert == "inches":

print "The total is" + `A`

elif convert == "centimeters":

print "The total is" + `B` + "centimeters"

raw_input ("Press <enter>")

But in this, when I try to run the module, it skips the middle part and just ends with:

Unit conversion

What would you like to convert the number of feet to? inches

How many feet are there? 2

Press <enter>

2 Answers

Relevance
  • 8 years ago

    Although I am not Python Programmer !! The mistake is in Str function

    U Pass Not Defined variable , in other word inches here is a variable not text Put some quotes to make it text ( U can use it with out str function )

    I hope this helped

  • 5 years ago

    This sounds good

Still have questions? Get your answers by asking now.