On Wed, Mar 19, 2008 at 3:56 PM, Tim Michelsen <<a href="mailto:timmichelsen@gmx-topmail.de">timmichelsen@gmx-topmail.de</a>> wrote:<br><div class="gmail_quote"><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
m = 0<br>
cm = 0<br>
mm = 0<br>
## first list item<br>
if first.endswith("m",-1):<br>
m = first.strip("m")<br>
elif first.endswith("cm",-2):<br>
cm = first.strip("cm")<br>
elif first.endswith("mm",-2):<br>
mm = first.strip("mm")<br>
else:<br>
print 'Wrong unit!'<br>
## second list item<br>
if second.endswith("m",-1):<br>
m = second.strip("m")<br>
elif second.endswith("cm",-2):<br>
cm = second.strip("cm")<br>
elif second.endswith("mm",-2):<br>
mm = second.strip("mm")<br>
else:<br>
print 'Wrong unit!'<br>
## third list item<br>
if second.endswith("m",-1):<br>
m = second.strip("m")<br>
elif second.endswith("cm",-2):<br>
cm = second.strip("cm")<br>
elif second.endswith("mm",-2):<br>
mm = second.strip("mm")<br>
else:<br>
print 'Wrong unit!'<br>
</blockquote><div><br>Since they all end in "m", if you do that test first it catches all three cases. It's almost as if you put the "else" before the "if".<br>Regexes are a wonderful thing to learn - do that by all means - but the simplest thing would be to reverse the order of your tests: check against "mm" first, then "cm", THEN "m".<br>
<br>To clarify - regexes are probably the best solution for your current program, but as a general rule it helps to look at tests like this from a different angle.<br></div></div><br>-- <br><a href="http://www.fsrtechnologies.com">www.fsrtechnologies.com</a>