Python converter

python

#1

miles to km converter including an input(). :

enter code here

 def miles_to_km_converter (ans):  
 ans = input('please enter amount of miles you wish to convert:') 
 print ((`enter code here`'you have entered:') + str(ans) + ' miles')
 return int(ans)*1.6 `enter code here`
 m == ans 
 a = miles_to_km_converter(m) 
 print ("The conversion equals:") print (str(a) + ' km')

I am receiving an error: “m not defined”. I cant understand whats wrong, I defined m as equal to the raw input ‘ans’. Thanks


#2
def miles_to_km_converter (ans):
    return int(ans) * 1.6

m = input('please enter amount of miles you wish to convert:')
a = miles_to_km_converter(m)
print("The conversion equals: " + str(a) + ' km')

When assigning m to ans you should use m = ans
The == operator is used to compare if to variables are equal

In your program the == operator is used before m is assigned a value so the program throws the error


#3

Your function is responsible for too many things.

def milesToKM(miles)
  return miles * 1.6
end

def kmToMiles(kilometers)
  return kilometers * 0.62
end

gather their raw input outside the function, pass the function their answer, then print the result.


#4

Opinion

When distances are vast, too much rounding can result in substantial errors. Since our computer is capable of working with very precise numbers with an error factor of around 10 to the minus 15, we should write the most precise numbers available into our constants.

1 foot = 0.3048 meters

Given that significant figures is a part of scientific calculation, we can arbitrarily multiply both sides by 1000 so both values have four significant figures.

 1000 feet = 304.8 meters.

Now we know that one mile is 5280 feet.

 5280 / 1000 = 5.280

so that.

1 mile = 5.280 * 304.8 meters  =>  1609.344 meters

Here is where sigdigs come around to bite us. There are way more digits in the result than we can use in a published solution. But we are talking about vast distances so lets arbitrarily increase 1 mile to 1 thousand miles.

 1000 miles = 1609344 meters

Now we can round out the significant figures in our result to 1.609 * 10 ** 6.

1609 kilometers is 344 meters short of 1000 miles.

The point here is that our conversion factors should be as precise as we can make them, and rounding left to the last step, when we publish our solution.

 1.609344 kilometers = 1 mile

The same would apply to converting from kilometers to miles. Write the best number you have in your conversion factors and let the function return as nearly precise a value as possible, then let the rounding happen as needed to meet sigdig requirements. Don’t round conversion factors or your program will never output more than an estimation of approximate conversion.