0

I am trying to create a miles to km converter including an input().

def miles_to_km_converter (ans):
    ans = input('please enter amount of miles you wish to convert:')
    print (('you have entered:') + str(ans) + ' miles')      
    return int(ans)*1.6 
    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 can't understand whats wrong, I defined m as equal to the raw input ans. Thanks

andrew_reece
  • 20,390
  • 3
  • 33
  • 58
  • 2
    Please format your code as code, so that others can read it. – Dragonthoughts Jul 20 '18 at 08:23
  • 1
    You haven't defined `m` anywhere. `==` is not assignment, it's a boolean test of equivalency. And that statement comes after your `return` anyway, which means it isn't evaluated. Also, the value `m` you want to pass into `miles_to_km_converter()` is represented by the `ans` argument within the function. Consider using a different name for your argument and for the `input()` value. – andrew_reece Jul 20 '18 at 08:25

2 Answers2

1

m is only defined within your function, so outside not available. I think you mixed up a little which code should be part of the function and which should be part of the main program.

Consider this:

def miles_to_km_converter (m):
    return float(m)*1.6 

ans = input('please enter amount of miles you wish to convert: ')
a = miles_to_km_converter(ans)

print ('The conversion equals: ', str(round(a, 3)) + ' km')

EDIT:

If you want the user prompt be part of the function:

def miles_to_km_converter():
    m = input('please enter amount of miles you wish to convert: ')
    return float(m)*1.6 

a = miles_to_km_converter()

print ('The conversion equals: ', str(round(a, 3)) + ' km')
SpghttCd
  • 10,510
  • 2
  • 20
  • 25
0

In your function -

def miles_to_km_converter (ans):
    ans = input('please enter amount of miles you wish to convert:')
    print (('you have entered:') + str(ans) + ' miles')      
    return int(ans)*1.6 
    m == ans # let's call this line 1

Before line 1 you have made a return call. This call stops the the execution, returns what you want to return and exits the function. After return, nothing gets executed. See here

Also, on line 1, what we are doing is comparing m with ans. This says -

check if ans is equal to a

See python comparison operators

What I think you want is something like this -

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

Why don't we need parameter to be passed to this function?

Our function miles_to_km_converter does not need a variable to be passed to it from rest of the function. The purpose of our function is

take input -> convert it -> return it

But if we had a situation where we wanted to pass a variable from the rest of the script to the function, we would need a parameter. Read here

For example -

def miles_to_km_converter (ans):
    print (('you have entered:') + str(ans) + ' miles')      
    return int(ans)*1.6

ans = input('please enter amount of miles you wish to convert:')
# Now our variable `ans` is not accessible to the function 
# We need to explicitly pass it like -
a = miles_to_km_converter (ans)
# And then we'll print it 
print ('The conversion equals: ', str(round(a, 3)) + ' km')

I hope this answers your question

Sushant
  • 3,499
  • 3
  • 17
  • 34
  • I see. Can you explain please once again why there is no need in parameters when using a input as part of the function? – user10082181 Jul 20 '18 at 09:14