2

I am new to this site, but wanted to see if anyone could help me here

I am a newbie when it comes to programming, and I am currently trying to make a miles to km / km to miles program, which I based on a tutorial video I watched that taught functions.

I've written this short program successfully in which you can type in a specific amount of miles to convert to kilometer:

def miles_to_km(miles):
    amount = miles / (1/1.609344)
    print(miles, "miles are equal to", format(amount, '.2f'), "kilometers" )

miles = float(input("Please type in the amount of miles you would like to  convert"))

miles_to_km(miles) 

but I want to give users the choice of converting from miles to km or the other way around. This is my code now. Sorry if it's unorganized, I'm am a newbie

def miles_to_km():
    miles = float(input("Please type in the amount of miles you would like to convert"))
    amount = miles / (1/1.609344)
    print(miles, "miles are equal to", format(amount, '.2f'), "kilometers")


def km_to_miles():
    km = float(input("Please type in the amount of kilometers you would like  to convert"))
    amount = km * (1/1.609344)
    print(km, "kilometers are equal to", format(amount, '.2f'), "kilometers")

print("Hello, would you like me to convert values from miles to kilometers or from kilometers to miles?")
question = float(input("Please press 1 for miles to km conversion, otherwise press 2 for km to miles conversion"))

if question is 1:
    miles_to_km()
elif question is 2:
    km_to_miles()

I don't get errors when running the program, but it finishes after asking the user to press 1 or 2 to choose which way they want to convert, I need help with running either miles_to_km or km_to_miles based on user input.

Hope somebody can help me, it is probably not difficult, but I couldn't figure it out.

MechMayhem
  • 75
  • 7
  • possible duplicate of [String comparison in Python: is vs. ==](http://stackoverflow.com/questions/2988017/string-comparison-in-python-is-vs) – Morgan Thrapp Aug 07 '15 at 16:18
  • Why `miles / (1/1.609344)` & `km * (1/1.609344)`? `miles * 1.609344` or `km/1.609344` would make more sense. – SiHa Aug 07 '15 at 16:59
  • Yeah, I guess you are right. Heh, I actually didn't think about that – MechMayhem Aug 08 '15 at 17:04

2 Answers2

2

is operator is used for checking identity (meaning they check if the operands are the same object), not equality . You should use == , not is . The issue occurs because you are converting the user input to float , whereas you are checking it against int, they would never be identical objects, Example -

>>> 1 is 1.0
False
>>> 1 == 1.0
True

Whereas when you check for equality, they are fine. Also, converting the choices to float does not make much sense to me, you may be better off converting them to int -

question = int(input("Please press 1 for miles to km conversion, otherwise press 2 for km to miles conversion"))
Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176
0

As @MorganThrapp pointed out, your issue is in the use of is instead of ==

if question is 1:
    miles_to_km()
elif question is 2:
    km_to_miles()

Will compare is the question variable is the same object as 1 or 2

By using:

if question == 1:
    miles_to_km()
elif question == 2:
    km_to_miles()

You will be comparing the values, and should get the correct result.

See here for more.

EDIT: @siha I think it should work:

will@will-mint2 ~ $ python
Python 2.7.9 (default, Mar  1 2015, 12:57:24) 
[GCC 4.9.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> a = float(input("enter num:"))
enter num:1
>>> a == 1
True
>>> a is 1
False
Community
  • 1
  • 1
Will
  • 4,299
  • 5
  • 32
  • 50