break与else位置不同,结果也不同 。
a = ('milk','apple','pear','strawberry')
for food in a:
if food == 'apple':
print ('yes,you get it')
break
else :
print ('no')
-->no
-->yes,you get it
a = ('milk','apple','pear','strawberry')
for food in a:
if food == 'apple':
print ('yes,you get it')
else :
print ('no')
-->no
-->yes,you get it
-->no
-->no
a = ('milk','apple','pear','strawberry')
for food in a:
if food == 'pear':
print ('yes,you get it')
else :
print ('no')
-->yes,you get it
-->no
a = ('milk','apple','pear','strawberry')
for food in a:
if food == 'b':
print ('yes,you get it')
else :
print ('no')
-->no
a = ('milk','apple','pear','strawberry')
for food in a:
if food == 'apple':
print ('yes,you get it')
break
else :
print ('no')
-->yes,you get it