471,336 Members | 1,390 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,336 software developers and data experts.

name is not defined

cwoozy
5
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:
Expand|Select|Wrap|Line Numbers
  1. import math
  2. def sphere():
  3.     Radius = input("Please enter the radius of the sphere and press Enter: ")
  4.     Volume = (4/3*math.pi*Radius**3)
  5.     Area = (4*math.pi*Radius**2)
  6.     Print Volume
  7.     Print Area
  8. 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"):

Expand|Select|Wrap|Line Numbers
  1. def main():
  2.     print "This Program illustrates a chaotic function."
  3.     x = input("Enter a number between 0 and 1: ")
  4.     for i in range (10):
  5.         x = 3.9 * x * (1-x)
  6.         print x
  7.  
  8. 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!
Sep 18 '07 #1
2 6935
bartonc
6,596 Expert 4TB
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:
Expand|Select|Wrap|Line Numbers
  1. import modulename  # leave off the .py extention
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".]
Sep 18 '07 #2
cwoozy
5
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
Sep 18 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

5 posts views Thread by David Rasmussen | last post: by
4 posts views Thread by IMS.Rushikesh | last post: by
7 posts views Thread by Deepak Jharodia | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.