473,394 Members | 1,887 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 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 7443
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

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

Similar topics

2
by: Liang | last post by:
Hi, I use "defined $r_libs->{$name}" to check first if a key exists in a hash table. But Perl gives a warning WHENEVER the key exists: "Use of uninitialized value". Would u please help to...
5
by: David Rasmussen | last post by:
If I have a string that contains the name of a function, can I call it? As in: def someFunction(): print "Hello" s = "someFunction" s() # I know this is wrong, but you get the idea... ...
4
by: IMS.Rushikesh | last post by:
Hi All, I am trying to execute below code but it gives me an COMException ///// Code Start //// public string GetName(Excel.Range range) { try { if (range.Name != null)
20
by: weston | last post by:
I've got a piece of code where, for all the world, it looks like this fails in IE 6: hometab = document.getElementById('hometab'); but this succeeds: hometabemt =...
7
by: Deepak Jharodia | last post by:
I'm using a templatized class in GCC based environ template<class A, class B> class foo {... ....} F; Now I want to know that particular instance of this class was instantiated with what...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.