I'm experiencing a very odd problem in Python. These two didn't seem to help:
Validating user input strings in Python and fastest way to compare strings in python
I've written the following small program, test.py:
import sys
while True:
print("\n")
print("Type exit to exit: ")
inputvar = str(input())
print("Input: "+inputvar)
if inputvar == 'exit':
print("Exiting!")
sys.exit("\n\nProgram quit.")
else:
print("Still here")
The current version doesn't seem to quit with exit, 'exit' or "exit". Using input() instead of str(input()) doesn't seem to make a difference, and using is or in instead of == doesn't make a difference either.
I'm using Python 3.2 on Windows 7.
input()already returns a string, sostr(input())won't make any difference.iscompares identity, not equality, so is a bad choice for string comparisons;==is correct. If you're saying that'exit' != 'exit'in your code, you have a serious problem; are you sure you haven't introduced some whitepace? Tryinputvar.strip()orprint(repr(inputvar))to see what's going on.inputvar.strip()just in case there is trailing whitespace for some reason.