473,378 Members | 1,407 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,378 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 7437
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: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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: 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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.