-1

Why does this give me an error when calc == off? The program should end after i is changed.

def calc():

i="i"

while i=="i":
    calc = input("Enter your calculation ")

    if calc!="off" or "Off":
        ans = eval(calc)
        print(ans)

    else:
        i="a"
user3548949
  • 43
  • 1
  • 1
  • 3

1 Answers1

1

The issue is that you're evaling the string in calc. That's looking for a local variable named "off" which doesn't exist.

While we're on the subject of bad syntax, this doesn't work:

if calc!="off" or "Off":

You want this instead:

if calc not in ("off", "Off"):

or ideally:

if calc.lower() != "off":
g.d.d.c
  • 46,865
  • 9
  • 101
  • 111
  • That totally did it thanks. So out of curiosity what is "or" for? – user3548949 Oct 17 '14 at 16:02
  • Thanks for downvoting and criticizing me but pointing out the same mistake in your own answer. – carmenism Oct 17 '14 at 16:07
  • @vulpix - pointing out the logic error was a secondary concern. Your answer did not answer the question, which was "what is producing my NameError?". If your answer does not _answer the question_ it deserves downvotes. – g.d.d.c Oct 17 '14 at 16:11