I have found a function which implement a switch statement –>
File = open('/file.txt','r')
String = File.readline()
String = str(String)
print String
for case in switch(String):
if case("Head"):
print "test successed"
break
if case("Small"):
print String
break
if case("Big"):
print String
break
if case():
print String
break
the String value when I print it is Head, but the switch statement always goes to the last case.. the function works fine apparently because when I changed the String with v = “Head” it worked!!!
any idea what went wrong ?
the switch function –>
class switch(object):
def __init__(self, value):
self.value = value
self.fall = False
def __iter__(self):
"""Return the match method once, then stop"""
yield self.match
raise StopIteration
def match(self, *args):
"""Indicate whether or not to enter a case suite"""
if self.fall or not args:
return True
elif self.value in args: # changed for v1.5, see below
self.fall = True
return True
else:
return False
Please don’t ever write code like this. Do it in proper Python style. It’s much easier to read. Anyone coming across usage of that “switch” mess will probably curse you heartily.
As to why it’s failing, the
readline()call will very likely be including a trailing newline character, and'Head' != 'Head\n'.