class Player(object):
    def __init__(self):
        self.points = 0

def add_points():
    plyr1.points += 1
    plyr2.points += 1
    plyr3.points += 1

if __name__ == '__main__':
    (plyr1, plyr2, plyr3 = (Player(), Player(), Player())
     add_points()

How could the 3 lines in add_points() be written better/shorter?

Haha ! never use variable1, variable2, variable3, use a list called variable

class Player(object):
    def __init__(self):
        self.points = 0

def add_points():
    for p in plyr:
        p.points += 1

if __name__ == '__main__':
    plyr = [Player() for i in range(3)]
    add_points()

The players are now plyr[0], plyr[1], plyr[2].

thx.

then a way to add names would be this?

class Player(object):
    def __init__(self):
        self.points = 0
        self.name = 'none'

def add_points():
    for p in plyr:
        p.points += 1

def add_name():
    names = ['player 1', 'player 2', 'player 3']
    n = 0
    for p in plyr:
        p.name = names[n]
        n +=1

if __name__ == '__main__':
    plyr = [Player() for i in range(3)]
    add_points()
    add_name()

thx.

then a way to add names would be this?

class Player(object):
    def __init__(self):
        self.points = 0
        self.name = 'none'

def add_points():
    for p in plyr:
        p.points += 1

def add_name():
    names = ['player 1', 'player 2', 'player 3']
    n = 0
    for p in plyr:
        p.name = names[n]
        n +=1

if __name__ == '__main__':
    plyr = [Player() for i in range(3)]
    add_points()
    add_name()

It's a solution, other solutions are

def add_name():
    names = ['player 1', 'player 2', 'player 3']
    for n, p in enumerate(plyr):
        p.name = names[n]

# or

def add_name():
    names = ['player 1', 'player 2', 'player 3']
    for name, p in zip(names, plyr):
        p.name = name

how about for combining two 'for' loops?

for i in a:
    i + 1
for i in b:
    i + 1

These two loops do nothing so they simplify to just.

pass

indent the second loop ;)

ah, i needed to write this example instead:

for i in a:
    i.points = 10
for i in b:
    i.points = 10

looks like it should be:

c = a + b
for i in c:
    i.points = 10

how about checking a list for all the values you specify?
this works, but seems too long:

def check_list(list):
    """ checks to see if 1, 2, and 3 are in the list and returns True if so """
    if 1 in list[0]:
        if 2 in list[1]:
            if 3 in list[2]:  return True
        elif 3 in list[1]:
            if 2 in list[2]:  return True
    elif 2 in list[0]:
        if 1 in list[1]:
            if 3 in list[2]:  return True
        elif 3 in list[1]:
            if 1 in list[2]:  return True
    elif 3 in list[0]:
        if 1 in list[1]:
            if 2 in list[2]:  return True
        elif 3 in list[1]:
            if 1 in list[2]:  return True
    else:  return False

myList = [3, 2, 1]
a = check_list(myList)
print a

ah, i needed to write this example instead:

for i in a:
    i.points = 10
for i in b:
    i.points = 10

looks like it should be:

c = a + b
for i in c:
    i.points = 10

how about checking a list for all the values you specify?
this works, but seems too long:

def check_list(list):
    """ checks to see if 1, 2, and 3 are in the list and returns True if so """
    if 1 in list[0]:
        if 2 in list[1]:
            if 3 in list[2]:  return True
        elif 3 in list[1]:
            if 2 in list[2]:  return True
    elif 2 in list[0]:
        if 1 in list[1]:
            if 3 in list[2]:  return True
        elif 3 in list[1]:
            if 1 in list[2]:  return True
    elif 3 in list[0]:
        if 1 in list[1]:
            if 2 in list[2]:  return True
        elif 3 in list[1]:
            if 1 in list[2]:  return True
    else:  return False

myList = [3, 2, 1]
a = check_list(myList)
print a

Use the builtin function all

a = all(x in mylist for x in [1, 2, 3])

There is also

a = set([1,2,3]).issubset(set(mylist))

cool.

can the return statement be written any better in the following:

def breakup(mylist):
    """ output: (['a', 'b', 'c'], ['1', '2', '3']) """
    return [mylist[0][0], mylist[1][0], mylist[2][0]], [mylist[0][1], mylist[1][1], mylist[2][1]]

mylist1 = ['a1', 'b2', 'c3']
a = breakup(mylist1)

cool.

can the return statement be written any better in the following:

def breakup(mylist):
    """ output: (['a', 'b', 'c'], ['1', '2', '3']) """
    return [mylist[0][0], mylist[1][0], mylist[2][0]], [mylist[0][1], mylist[1][1], mylist[2][1]]

mylist1 = ['a1', 'b2', 'c3']
a = breakup(mylist1)

Yes,

>>> mylist = ['a1', 'b2', 'c3']
>>> zip(*mylist)
[('a', 'b', 'c'), ('1', '2', '3')] # cool, but it's a list of tuples
>>> [list(x) for x in zip(*mylist)]
[['a', 'b', 'c'], ['1', '2', '3']] # now a list of lists
>>> tuple(list(x) for x in zip(*mylist))
(['a', 'b', 'c'], ['1', '2', '3']) # a tuple of lists

ty

how about this If statement:

if player[0].score == 10 and (player[1].score == 20 and player[2].score == 20 and player[3].score == 20):

ty

how about this If statement:

if player[0].score == 10 and (player[1].score == 20 and player[2].score == 20 and player[3].score == 20):

One way is

if all(player[i].score == score for i, score in enumerate((10,20,20,20))):

how about:

if all(player[i].score == score for i, score in enumerate((10,20,20,20))):
    print 'player[0]'s score is 10, all other's are 20'
if all(player[i].score == score for i, score in enumerate((20,10,20,20))):
    print 'player[1]'s score is 10, all other's are 20'
if all(player[i].score == score for i, score in enumerate((20,20,10,20))):
    print 'player[2]'s score is 10, all other's are 20'
if all(player[i].score == score for i, score in enumerate((20,20,20,10))):
    print 'player[3]'s score is 10, all other's are 20'

how about:

if all(player[i].score == score for i, score in enumerate((10,20,20,20))):
    print 'player[0]'s score is 10, all other's are 20'
if all(player[i].score == score for i, score in enumerate((20,10,20,20))):
    print 'player[1]'s score is 10, all other's are 20'
if all(player[i].score == score for i, score in enumerate((20,20,10,20))):
    print 'player[2]'s score is 10, all other's are 20'
if all(player[i].score == score for i, score in enumerate((20,20,20,10))):
    print 'player[3]'s score is 10, all other's are 20'

You can try

scores = list(p.score for p in player[:4])
if sorted(scores) == [10,20,20,20]:
    print "player[{0}]'s score is 10, all other's are 20".format(scores.index(10))

There may be other ways...

To manage a variable number of players you could write

scores = list(p.score for p in player[:4])
if set(scores) == set([10,20]) and sum(scores) + 10 == 20 * len(scores):
    print "player[{0}]'s score is 10, all other's are 20".format(scores.index(10))

which permits to replace 4 with any other number >= 2.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.