name is not defined

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cwoozy
    New Member
    • Sep 2007
    • 5

    #1

    name is not defined

    Hello -

    I am very new to Python, and I am trying to write a program that returns the Area and Volume of a sphere when the user inputs the radius. I thought I had a solid program while typing it up:
    Code:
    import math
    def sphere():
        Radius = input("Please enter the radius of the sphere and press Enter: ")
        Volume = (4/3*math.pi*Radius**3)
        Area = (4*math.pi*Radius**2)
        Print Volume
        Print Area
    sphere()
    But when I try to run the program, I recieve this error message:

    Traceback (most recent call last):
    File "<pyshell#0 >", line 1, in -toplevel-
    sphere()
    NameError: name 'sphere' is not defined

    Upon further instpection, when I try to run the chaos.py program (taken directly from Zelle's "Python Programming: An Introduction to Computer Science"):

    Code:
    def main():
        print "This Program illustrates a chaotic function."
        x = input("Enter a number between 0 and 1: ")
        for i in range (10):
            x = 3.9 * x * (1-x)
            print x
    
    main()
    I get this error again:
    Traceback (most recent call last):
    File "<pyshell#0 >", line 1, in ?
    main()
    NameError: name 'main' is not defined


    Any suggestions would be much appreciated.

    I'm using 2.4.4 on Windows Vista.

    Thanks!
  • bartonc
    Recognized Expert Expert
    • Sep 2006
    • 6478

    #2
    That would be the Python "shell" that you are typing into.
    In order to get your program into the shell's namespace, you must use:[CODE=python]import modulename # leave off the .py extention[/CODE]But that's not the best for learning.
    Most IDEs (are you using IDLE for your edits?) have the ability to run the script that is being edited. That is the easiest way to get started. Alternatively, you can use an OS command shell. (on XP, anyway) it's
    > modulename.py
    provided that your environment is set up.

    Let us know if you need anything to get set up.

    [EDIT: I see that you are using IDLE. I think that the menu item to look for is called "Run".]

    Comment

    • cwoozy
      New Member
      • Sep 2007
      • 5

      #3
      Thanks -
      Run -> Run Module instead of Run -> Python Shell

      Also, noticed I was using "Print" instead of "print" - used to Visual Basic, that will throw me off for a while.

      cwoozy

      Comment

      Working...