How do I fix this "Name is not Defined"?

3 step approach to problem solving
how to fix a problem
problems to fix
how to fix youtube on computer
how do you fix youtube
fix a problem synonyms
how to deal with a problem you can t fix
fix the problem quotes

Just wondering, how can I fix this? I can't get it to work no matter what I do. Since I've started using Python my main problem is calling variables, exactly how do I do it? For example in this case how do I fix it, or in another code how would I call a variable from one section to another?

I get a

Traceback (most recent call last): File "C:/Users/Test.py", line 12, in while guess != number: NameError: name 'guess' is not defined

Error

As a side note, why does using Global X = Global X + 1 not work?

import random

def main():
 guessesTaken = 0
 guess = 0
 number = random.randint (1, 1000)
 print('Guess a number from 1 to 1000. debug=',number,)
 guess = float(input('  '))
 Hot = number + 10
 Cold = number - 10

while guess != number:

 guessesTaken = guessesTaken + 1   
 if guess > number:
  print('Too High!')

 elif guess < number:
  print('Your guess is too low.')

 elif guess < Hot > number:
  print('Your guess Hot!')

 elif guess < Cold > number:
  print('Your guess is Cold!.')

 else:
  print('Error')

while guess == number:
  print('You got it!, you guessed the number in' + guessesTaken + ' guesses!')

main()

Your specific issue of NameError: name 'guess' is not defined is because guess is defined in your main function, but the while loop that it is failing on is outside of that function.

Your indention is entirely wrong for this application.

If you want your while guess != number: to work, you need to make it part of main

def main():
    guessesTaken = 0
    guess = 0
    number = random.randint (1, 1000)
    print('Guess a number from 1 to 1000. debug=',number,)
    guess = float(input('  '))
    Hot = number + 10
    Cold = number - 10

    while guess != number:
        ...

You have other indention problems all over too. Within main your lines are indented 5 spaces (not the PEP8 prescribed 4 spaces).

Your while loop isn't going to do what you are hoping. If a user doesn't get the answer right on the first try, it is going to hit your while guess != number: and loop forever. To resolve this, your input should occur within the loop as well. The second while loop while guess == number: would hit the same infinite loop. Why does this need to print repeatedly? It could work just fine as an if

My network is blocking Seesaw. How do I fix this? – Seesaw Help , My network is blocking Seesaw. How do I fix this? Avatar. Emily Seesaw Support. How do I unblock Seesaw at school? Most schools can get going with Seesaw  Before carrying out this procedure, disconnect from the Internet. That means turning off Wi-Fi and disconnecting from a wired connection. If you don't, Windows 10 will indicate some files are in use and cannot be modified or the command will be non-responsive. Also, you should restart your computer after disconnecting from the Internet. The

In python, the indentation determines the code block (and thus scope).

So, in your code, when you unindent for your while loops:

def main():
   guessesTaken = 0
   guess = 0
   number = random.randint (1, 1000)
   print('Guess a number from 1 to 1000. debug=',number,)
   guess = float(input('  '))
   Hot = number + 10
   Cold = number - 10

while guess != number:   # The unindent here 
   guessesTaken = guessesTaken + 1
   ...

The interpreter treats Cold = number - 10 as the end of main(). So, your loop is not a part of main() and can't access variables that are local to main (such as guess).

My guess is your intention was to have both while loops part of main. So they should be indented to match:

import random
def main():
     guessesTaken = 0
     guess = 0
     number = random.randint (1, 1000)
     print('Guess a number from 1 to 1000. debug=',number,)
     guess = float(input('  '))
     Hot = number + 10
     Cold = number - 10

     while guess != number:
         guessesTaken = guessesTaken + 1
         if guess > number:
             print('Too High!')
         elif guess < number:
            print('Your guess is too low.')
         elif guess < Hot > number:
            print('Your guess Hot!')
         elif guess < Cold > number:
            print('Your guess is Cold!.')
         else:
            print('Error')

     while guess == number:
         print('You got it!, you guessed the number in' + guessesTaken + ' guesses!')

main()

Although, as is your code has other issues; it gives me an infinite loop.

5 Ways To Fix YouTube, If you are having trouble with a file upload, you can't click save, nothing loads when you launch a course, or any other inconvenience, iFixit is a global community of people helping each other repair things. Let's fix the world, one device at a time. Troubleshoot with experts in the Answers forum—and build your own how-to guides to share with the world.

Judging from the indentation of your code, I am guessing that the only problem here is that you have not indented the while loop.

Indentation in python is very important. Here is the corrected code :

import random


def main():
     guessesTaken = 0
     guess = 0
     number = random.randint (1, 1000)
     print('Guess a number from 1 to 1000. debug=',number,)
     guess = float(input('  '))
     Hot = number + 10
     Cold = number - 10

     while guess != number:

         guessesTaken = guessesTaken + 1

         if guess > number:
             print('Too High!')


         elif guess < number:
             print('Your guess is too low.')


         elif guess < Hot > number:
            print('Your guess Hot!')


         elif guess < Cold > number:
            print('Your guess is Cold!.')

         else:
            print('Error')

What seems to be happening with your current code is that the while loop gets called even before your main() function is called. Therefore 'guess' and 'number' both do not exist in that context.

How to Fix Everything, If it's only a few apps, you might be able to repair the situation by deleting the data they store on your phone. ADVERTISEMENT / ADVERTISE  Right-click the Taskbar in a blank area, and select Show touch keyboard button. A new icon of a little keyboard will appear next to the clock in the taskbar. Tap it anytime with the mouse cursor to

Basic Troubleshooting "How do I fix this?" – TargetSolutions Support, Wrong question, sorry: I understand that you think you might be broken, but "how can I fix myself?" is the wrong question. All answers to that question share the  “How Do We Fix It?” is a weekly, half-hour podcast about solving problems ranging from the geo-political to the personal. Veteran journalists Richard Davies and Jim Meigs interview the smartest minds in the field, and look for real-world solutions.

How to fix the most common smartphone problems, Hi all,. The Chrome team recently added these new sub-error codes to the "Aw, Snap!" error page to better diagnose the cause of crashes in  Thousands of repair manuals, tutorials, and how-to guides for DIY fixes. From electronics to cars, learn how to repair your own stuff and save yourself some money.

Why am I broken and how can I fix myself?, How do I fix this? Answer -. To resolve the 400 Bad Request Error you are experiencing, you simply need to clear your browser's Cookies and  If a Startup Repair didn't do the trick, or the problem you're trying to fix isn't related to Windows starting properly, then Reset This PC is your next best bet. Reset This PC in Windows 10 & 8: A Complete Walkthrough

Comments
  • Is that the real indentation of the file?
  • what version of Python are you using?
  • 3.3 and Not really, the page just asked me to add 4 indentations to post it, but that isn't the problem. I get a Traceback (most recent call last): File "C:/Users/TEST.py", line 12, in <module> while guess != number: NameError: name 'guess' is not defined >>>
  • Do you realize that indentation means ALOT in python? If you don't post the exact code you have with the exact indentation it is hard to help you.