-1

I am running python 2.75 on Wing IDE

The code:

exit = False

while not exit:

    selection = int(raw_input("Press 1 to go and 0 to quit: ")
    if selection == 1:
       print("yay")
    elif selection == 0:
       print("Goodbye")
       exit = True
    else:
       print("Go away")

When I press 0, it says:

local variable 'exit' referenced before assignment

What is wrong?

user3595018
  • 3
  • 1
  • 5

2 Answers2

2

Your code works fine as follows:

exit = False

while not exit:
    selection = int(raw_input("Press 1 to go and 0 to quit: "))       #added ) to correct syntax error
    if selection == 1:
        print("yay")
    elif selection == 0:
        print("Goodbye")
        exit = True
    else:
        print("Go away")

DEMO

sshashank124
  • 31,495
  • 9
  • 67
  • 76
  • Congrats for your 10k rep status ;) – Amit Joki May 03 '14 at 07:26
  • you can view [this](http://stackoverflow.com/tools)! and deleted answers ;) – Amit Joki May 03 '14 at 07:27
  • @Downvoter, Can you please explain if there is anything wrong with my answer? Thank you. – sshashank124 May 03 '14 at 07:32
  • I didn't down-vote, but I think some explanation is necessary. Currently, it looks like you have exactly the same code as OP. This may not be your fault, but in any case, it is good to explain *why* some code fixes a particular problem. I'm being picky, there's not much you can do to make this useful to the community anyway, given the question :-) – juanchopanza May 03 '14 at 07:36
  • @juanchopanza, Thank you, I have commented the corrections – sshashank124 May 03 '14 at 07:39
0

You're missing a close parens around your int()

Still, if you're using a while loop, why not simply used a break instead of a boolean? It's much cleaner. Instead of exit = True, simply type break and the loop will end.

if my understanding of what you've typed is correct, you want to end up with:

    while True:
      selection = int(raw_input("Press 1 to go and 0 to quit: "))
      if selection == 1:
        print("yay")
      elif selection == 0:
        print("Goodbye")
        break
      else:
        print("Go away")
eriese
  • 175
  • 7