4

Im new to python and I've been given the task to code a centimetres to inches and vice versa program. The idea is a user enters a number, the program asks if they want to convert it from inches to centimetres or centimetres to inches and then displays the result.

Im using python 3.3.5 on a mac is that helps.

Here is my code: ( the commented out section shows the task and my first attempt)

##
##Write a program that converts between centimetres, and inches and vice versa, by:
##asking the user to input a number
##asking the user to choose between converting from centimetres to inches or from inches to centimetres
##calculating and outputting the result using functions


##### Old attempt
##print(" Program to convert beteween centimetres and inches " )
##
##centimetre = 1
##inch = centimetre * 2.45
##
##number = input (" Please enter a number: ")
##choice = input (" Do you want to convert into centimetres or inches: ")
##if choice == "centimetres" or "Centimetres":
##    print("Your number is", centimetre*number, "cm")
##else:
##    print("Your number is",number*inch, "inches")


number = int(input(" Please enter a number: "))
choice = input(str(" If you want to convert from centimetres to inches type 'A' \n If you want to convert from inches to centimetres type 'B': "))
if choice in "A" or "a":
    print(" Your number is" , number*2.54,"inches")
else:
    print(" Your number is", number/2.54, "centimetres")

Answer has been found, thank you for the very quick replies!

Isabel Secord
  • 43
  • 1
  • 7
  • You gotta do `if choice in ("A" , "a")` – Bhargav Rao Sep 24 '15 at 19:54
  • Just some tips, you might want to convert the input number with `float` rather than `int` as int will only know how to interpret integer values and float can handle decimals. You also don't have to convert anything in quotation marks to a string. It is a string already. This applies to `choice = input(str(" ...`. – zephyr Sep 24 '15 at 19:55
  • 1
    @IsabelSecord Ignore my answer (now deleted), your print works fine I actually didn't realize print could be used the way you did. – nash Sep 24 '15 at 20:00
  • I don't agree with the duplicate flag that was assigned to. I just read it and the two don't relate to each other in any way. – idjaw Sep 24 '15 at 20:02

1 Answers1

4

The condition if choice in "A" or "a": should be modified to, e.g., if choice in "Aa": so that you test whether choice is either 'A' or 'a'. The original version always evaluates to True.

ewcz
  • 12,819
  • 1
  • 25
  • 47