Deathhound
Deathhound

Reputation: 67

Python if in string multiple times

Here's my script of sorting males and females one after another into the variable 'group':

males = ['John Smith 1 : M', 'John Smith 2 : M', 'John Smith 3 : M', 'John Smith 4 : M', 'John Smith 5 : M', 'John Smith 6 : M', 'John Smith 7 : M', 'John Smith 8 : M', 'John Smith 9 : M', 'John Smith 10 : M']
females = ['Jane Smith 1 : F', 'Jane Smith 2 : F', 'Jane Smith 3 : F', 'Jane Smith 4 : F', 'Jane Smith 5 : F', 'Jane Smith 6 : F']

group_number = 3
while int(group_number)%2 != 0:
    group_number = raw_input("What is the size of the group? (even numbers only)")

group = []
a=0

for a in range(max(len(males), len(females))):
    if a < len(males):
        group.append(males[a])
    if a < len(females):
        group.append(females[a])
    a=a+1

group = zip(*(iter(group),) * int(group_number))

print group

All I need to do now is split the group by an inputted number (even numbers only) and put the unbalanced male:female group in a separate group... I figured it out for 2, but it doesn't work for anything >2

n=0

for person in group:
    if " : M" and " : F" in str(person):
            print "Group",n,person
            print "--------------------------------------------------------"
    else:
        print "UNBALANCED",person
    n=n+1

Upvotes: 0

Views: 449

Answers (1)

realli
realli

Reputation: 1040

should be if " : M" in str(person) and " : F" in str(person):

what your code mean is:

if (" : M") and (" : F" in str(person) ):

and I think the a = a +1 in the for loop is not needed.

Upvotes: 1

Related Questions