작동하지 않습니다 - 나는 그것이 머리 인쇄 할 때>파이썬 switch 문은 내가 스위치 문 구현 함수 발견
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
문자열 값을하지만, 스위치 문은 항상 마지막 경우로 이동 .. 함수가 잘 작동합니다. 왜냐하면 내가 v = "Head"로 문자열을 변경했기 때문입니다!
어떤 아이디어가 잘못 되었나요?
스위치 기능 ->
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
당신은'case'와'switch' 객체/함수의 구현을 연결해야합니다. 그렇지 않으면 누구도 대답 할 수없는 질문을하고 있습니다. – ninjagecko
이 '스위치'기능을 어디에서 가져 보았습니까? 그 근원을 가리킬 수 있습니까? –
함수의 구현을 추가했습니다 – user573014