473,406 Members | 2,633 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,406 software developers and data experts.

NameError: global name 'x' is not defined

2
Hello I'm new to python (been using it all of 2 weeks) and I'm trying to write a program that compares the result of an equation using two different starting numbers that the user chooses. It also lets the user choose how many time it preforms the function and the amplitude of the function [a * x * (1 - x)]. here is the code. I'm using Vista 64 bit and Python v2.62.
Expand|Select|Wrap|Line Numbers
  1. def compare():
  2.     for i in range(num_iterations):
  3.             A = A * a * (1 - A)
  4.         B = B * a * (1 - B)
  5.     print A, B
  6.     def main():
  7.         a = input ("Enter the amplitude: ")
  8.         num_interations = input ("Enter the number of interations: ")
  9.         A = input ("Enter the starting value for the A series: ")
  10.         B = input ("Enter the starting value for the A series: ")
  11.     compare()
I keep getting the error:

Traceback (most recent call last):
File "<pyshell#46>", line 1, in <module>
compare()
File "<pyshell#45>", line 2, in compare
for i in range(num_iterations):
NameError: global name 'num_iterations' is not defined

Any help would be appreciated. Thanks!
Sep 9 '09 #1
2 44034
bvdet
2,851 Expert Mod 2GB
Please use code tags when posting code.

It's a matter of scope. You can learn about Python scope rules here. Add arguments to function compare, and pass the input from the user to the function. Your function definition main() is indented incorrectly.

Expand|Select|Wrap|Line Numbers
  1. def compare(n, a, A, B):
  2.     for i in range(n):
  3.         A *= a * (1 - A)
  4.         B *= a * (1 - B)
  5.     print A, B
  6.  
  7. def main():
  8.     a = input ("Enter the amplitude: ")
  9.     num_interations = input("Enter the number of interations: ")
  10.     A = input("Enter the starting value for the A series: ")
  11.     B = input("Enter the starting value for the B series: ")
  12.     compare(num_interations, a, A, B)
  13.  
  14. if __name__ == '__main__':
  15.     main()
Sep 9 '09 #2
Geared
2
Thanks for the help. That fixed it.
Sep 9 '09 #3

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

Similar topics

1
weaknessforcats
by: weaknessforcats | last post by:
C++: The Case Against Global Variables Summary This article explores the negative ramifications of using global variables. The use of global variables is such a problem that C++ architects have...
2
by: jolly | last post by:
hey guys, When i try to run my code I get an error. NameError name 'main is not defined' if __name__ == "__main__": main() filename = "addbook.dat"
2
by: pythonnewb | last post by:
I am fairly new to programming but have some very basic Java background. I am just learning python and tried to make a module that would allow me to create a file containing an address book. I was...
3
by: willkab6 | last post by:
I am very new to both programming and Pyhton and while trying to do some practice using A byte of python an Error pops up on the IDLE shell. I am using windows XP. PLease see below. while running:...
1
by: tshravan | last post by:
Hello All, I am newbie to python. I was trying to use a=zeros(5) in python shell, but it is throwing the following exception in the shell Traceback (most recent call last): File...
1
by: Lalit | last post by:
Hi I am very new to web development. I started with Pylons. I am using http://www.rexx.com/~dkuhlman/pylons_quick_site.html as reference to create a sample web page using pylons. I got stuck...
4
by: Harry | last post by:
Hi there. I am trying to download a file(sn.last) from a public FTP server with the following code: from ftplib import FTP ftp=FTP('tgftp.nws.noaa.gov') ftp.login()...
4
by: harijay | last post by:
Hi I am new to writing module and object oriented python code. I am trying to understand namespaces and classes in python. I have the following test case given in three files runner , master and...
4
by: jorgejch | last post by:
Hello, I've started with python (3) recently. Initialy only for scripting. Now I'm trying the object oriented bit. I'm getting the following error message <Atom.Atom object at 0x7f0b09597fd0>...
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: 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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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.