#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2007
    Location
    wichita, Kansas
    Posts
    37
    Rep Power
    0

    NameError: name 'guess' is not defined


    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:
    guess = int(raw_input('Enter an integer : '))

    if guess == number:
    print 'Congratulations, you guessed it.'
    running = False # this causes the while loop to stop
    elif guess < number:
    print 'No, it is a little higher than that.'
    else:
    print 'No, it is a little lower than that.'
    else:
    print 'The while loop is over.'
    # Do anything else you want to do here

    print 'Done'


    After typing the above as the book says, I get the error NameError: name 'guess' is not defined
    What Am I doing wrong?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2007
    Location
    NYC
    Posts
    46
    Rep Power
    7
    First of all post your code between "CODE" tags so the indentation shows. It's the hash symbol on the toolbar when you're posting.

    I fixed your code below:

    Code:
    running = True
    number = 27 # or some random number
    
    while running:
        guess = int(raw_input('Enter an integer : '))
    
        if guess == number:
            print 'Congratulations, you guessed it.'
            running = False # this causes the while loop to stop
        elif guess < number:
            print 'No, it is a little higher than that.'
        else:
            print 'No, it is a little lower than that.'
     
    print 'The while loop is over.'
    # Do anything else you want to do here
    
    print 'Done'
    Are you sure you entered it as written? Your version gives the error "NameError: name 'running' is not defined".... not "guess". That's because your first line makes reference to "running" before it has been assigned a value. "running" has to be True to start the "while" loop in the first place, so assign it "True" in the first line.

    Next you need to define "number" before the tests in the loop, otherwise you're going to get a "NameError: name 'number' is not defined". I assigned it the number 27 explicitly to save time, but you're going to want to use a function like "randrange" to generate a random number for the game.

    Also, you had the line "print 'The while loop is over'" within the while loop itself, when it should be outside of the while loop. So there was no need for the extra "else:"
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Posts
    1
    Rep Power
    0

    name


    I got the same error but its kinda different:

    hjallar@ubuntu:~/inf1100$ ls
    Gaussian_function1.py kick.py
    Python 2.7.1+ (r271:86832, Apr 11 2011, 18:05:24)
    [GCC 4.5.2] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> print Gaussian_function1.py
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    NameError: name 'Gaussian_function1' is not defined

    The file is there, dont see why it doesent work :S
    And if you think its something wrong with the program, here it is:

    #Oblig 1, 29/8 - 4/9
    #Evaluate a Gaussian Function

    from math import sqrt, exp, pi

    m=0
    s=2
    x=1

    z=(1/(sqrt (2*pi*s)))*(exp (-0.5*((x-m)/s)**2)

    print z

    But it looks like the error is in the name itself :S Help plz? :S
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,837
    Rep Power
    480

    Tell python about your module.


    Code:
    CLI_prompt$ ls
    module.py
    CLI_prompt$ python
    >>> import module
    >>> module.important_function(top,gun,args)
    I work!!!
  8. #5
  9. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,837
    Rep Power
    480

    pi is wrong


    http://tauday.com/

    Not a problem with your program, the problem is with pi itself.

    (There is a problem with your python, it's missing a `)'. You'll find that when you import it.)

IMN logo majestic logo threadwatch logo seochat tools logo