[Tutor] stopping a loop
Julieta Rangel
julieta_rangel@hotmail.com
Sat, 05 May 2001 14:28:09 -0500
Hello,
Here I am, bugging you guys ...AGAIN!. As some of you might remember, I'm
trying to write a program that determines whether a set is a group or not.
That is, given a finite set and a table, check for closure, associativity,
and make sure all elements in set have inverses and identity elements. This
is what I have so far:
import string
set = raw_input("Enter the elements of the set separated by a comma,ie,
a,b,c: ")
set = string.split(set, ",")
op = raw_input("Enter the binary operator sign, ie, * : ")
product = {}
for x in set:
for y in set:
prompt ="%s %s %s = ? "%(x,op,y)
z= raw_input(prompt)
product[x,y] = z
print " "
#Display the table
items = product.items()
items.sort()
for(x,y),z in items:
print x,op,y, " = ",z
I figure that if the user enters an element (when asked for the table
values), that is not in the set, there is no point in having the computer
ask for the remainder of the table because the set is not a group. I tried
inserting an "if" clause saying that if "z" is not in the set, print "The
set is not a group", and break the loop. The if clause worked well, if I
entered an element (when asked for the table values) that was not in the
set, the computer would inform me that the set is not a group because
closure fails; however, the result was not what I was looking for, the
computer would keep asking me for the rest of the table values. I read that
to break a loop I must insert a break statement, but I could not figure out
where to place it. I tried placing it in different places, but it wouldn't
work. Can anyone help me stop the loop as soon as a table value that is not
in the set is entered? The section in my book about breaking loops is not
very clear, at least not to me, so that's why you have me here, asking dumb
questions ...AGAIN.
julieta
_________________________________________________________________
Get your FREE download of MSN Explorer at http://explorer.msn.com