2

I am in a beginner programming class, and our instructor has chose Python as the language we are to work with. He has given us a very very simple assignment, which I will post, and the code that I have come up with, which I will also post. I feel like such a damn idiot because I cannot understand why this is not working. I keep getting a global definition error, and the teacher looks at us like we are the scum of the universe if we ask a question.

The assignment is:

Design a modular python program that asks the user to enter a distance in kilometers and then converts that distance to miles.

The program should loop for new input until told to stop by the person running the program.

hint: an input of zero (0) kilometers could be used to stop the program

The conversion formula is as follows: Miles = Kilometers * 0.6214

A minimum of three modules is required:

  1. input
  2. calculate
  3. print

And the code I came up with is:

def main(ans):
    while ans=='yes':
        data()
        calculate()
        words()

def data():
    print 'Enter Kilometers Please'    
    kilometers=input()
    return

def calculate():
    miles=kilometers*0.6214
    print miles

def words():
    print 'The number of miles is',
    print 'Enter another number?'

ans='yes'
main(ans)

Can anyone tell me what I am doing wrong? Please remember we are just starting out, and I do not know many of the advanced coding techniques I have seen in these forums.

Zero Piraeus
  • 56,143
  • 27
  • 150
  • 160
  • Read through a basic Python tutorial. You're missing out on a few key concepts here. Also, by "modules", did your instructor mean functions? – Blender Feb 17 '13 at 22:29
  • Yes. The instructor said that modules and functions are the same. I know I am missing out on something, and I will do as you say. – user2081458 Feb 17 '13 at 22:36
  • 3
    @user2081458 Sounds like your instructor is as clueless as they are unhelpful: [functions](http://docs.python.org/2/tutorial/controlflow.html#defining-functions) and [modules](http://docs.python.org/2/tutorial/modules.html) are very different in Python. – Zero Piraeus Feb 18 '13 at 05:08

2 Answers2

6

Running your program, the first error message we get is this:

Traceback (most recent call last):
  File "kilomiles.py", line 21, in <module>
    main(ans)
  File "kilomiles.py", line 4, in main
    calculate()
  File "kilomiles.py", line 13, in calculate
    miles=kilometers*0.6214
NameError: global name 'kilometers' is not defined

... which is very helpful: it tells you that the variable kilometers you attempted to use in line 13 of your program, in the calculate() function, isn't defined.

That's because of something called scope: the kilometers variable you defined in your data() function only exists inside that function.

So, you need to get the result

  1. out of data(), and
  2. into calculate().

To solve the first of these, you need to change data() so that it returns the value:

def data():
    print 'Enter Kilometers Please'
    kilometers = input()
    return kilometers

Now, in your main() function, you can capture the value it returns:

    kilometers = data()

Next, you need to change calculate() so that it accepts kilometers as an argument in the same way that main() accepts ans, and returns miles:

def calculate(kilometers):
    miles = kilometers * 0.6214
    return miles

Once that's done, you can change main() so that it passes kilometers into calculate(), and captures miles from it:

    miles = calculate(kilometers)

Finally, change words() so that it accepts miles and returns ans:

def words(miles):
    print 'The number of miles is', miles
    print 'Enter another number?'
    ans = input()
    return ans

... and make one more change to main() so that it passes miles into words(), and captures ans from it:

    ans = words(miles)

So your final program looks like this:

def main(ans):
    while ans == 'yes':
        kilometers = data()
        miles = calculate(kilometers)
        ans = words(miles)

def data():
    print 'Enter Kilometers Please'
    kilometers = input()
    return kilometers

def calculate(kilometers):
    miles = kilometers * 0.6214
    return miles

def words(miles):
    print 'The number of miles is', miles
    print 'Enter another number?'
    ans = raw_input()
    return ans

ans = 'yes'
main(ans)

The program now works, but it does have one problem that will come back and bite you if it becomes a habit: you're using input() instead of raw_input(). That's dangerous, and you should avoid it if at all possible. I'll leave it to you to work out how you can fix that, though.

Community
  • 1
  • 1
Zero Piraeus
  • 56,143
  • 27
  • 150
  • 160
  • 1
    +1 for going in-depth with your explanation and seriously helping out a newbie. Its disappointing when I see rude comments and a generally negative attitude toward new programmers. I'm sure its discouraging to those folks when they're treated that way. I'm glad to see answers like this! – That1Guy Feb 19 '13 at 22:31
-1

Its Simple. U declared or getting value of 'kilometers' in data() function and you are trying to access it from calculate(). calculate does not know what is 'kilometers'.. as it's local scope to data(). You should define kilometers and use it.

kilometers=0.0

def date():
   global kilometers
   ...
   ...
def calculate():
   global kilometers
   ...
Nikhil Rupanawar
  • 4,061
  • 10
  • 35
  • 51
  • 2
    This is *very bad advice*: A beginner should not be exposed to `global` until they know why it's almost always a bad idea. – Zero Piraeus Feb 18 '13 at 04:58