When data doesn't compare like you think it should, the first tool in your toolkit is the print statement. Just add print user_input
and you'll find its an instance of the usr
class, not the user input. You stored that in an instance variable called inp
. As an aside, in pythnon 2, you should inherit from object
to get a new-style class. You don't need to do that in python 3.
class usr(object):
def __init__(self, inp):
self.inp = inp
if inp == "abc":
print 0
exit()
user_input = usr(raw_input())
if user_input.inp == "123":
print("1")
else:
print("No")
If all you want to do is validate the input and not store it, a function is a better option. It can just return the validated string. It can do other prettying up of the data also. In this example, I also strip whitespace from front and back so minor input errors are ignored
def usr(inp):
inp = inp.strip()
if inp == "abc":
print 0
exit()
return inp
user_input = usr(raw_input())
if user_input == "123":
print("1")
else:
print("No")